2011-12-15 6 views
0

私はJavaプログラムを作成します。これは、あらかじめユーザーがログインする必要がある200個のユニークなページを解析します。 Chromeのデベロッパーコンソールを使用して特定のログインURL(https://r.espn.go.com/members/v3_1/login)を確認し、ログインプロセスでPOSTリクエストが使用されていることを確認し、ユーザー名(ユーザー名)とパスワード(パスワード)の両方についてフォームデータ名を確認しました。 this投稿者の指定したメソッドを使用して後続のリクエストにSESSIONID Cookieを取得すると、返されたヘッダーは大きく異なり、Cookieは返されませんでした。Jsoupを使用してsessionIdのクッキーを取得する際の問題

私もloginpageを返すJsoupとApacheの両方のHttpClientを、HttpPostを使用して、次のスニペットを試してみました、とのHttpResponseました:

MultipartEntity entity = new MultipartEntity(); 
entity.addPart("username", new StringBody(myUsername)); 
entity.addPart("password", new StringBody(myPassword)); 

HttpPost post = new HttpPost(url); 
post.setEntity(entity); 

HttpClient client = new DefaultHttpClient(); 
HttpResponse response = client.execute(post); 

String html = EntityUtils.toString(response.getEntity()); 

Document document = Jsoup.parse(html, url); 

私が読んだすべての例では、.PHPでのログインURLを持っています接尾辞、このメソッドは、PHPベースのログインサービスでのみ動作しますか?あるいは私は何か根本的に間違っているのですか?

ありがとうございます!

答えて

1

HttpClientがあなたのためにクッキー/セッションを管理するようにします。これが発生するには

  1. HttpContextを作成し、セッション/ Cookie管理がアクティブになるようにすべての要求に対して使用します。
  2. は、Cookieストア
  3. Exucuteあなたは

以下のHttpClient 4.1.xのバージョンのサンプルコードは、ステップ1で作成されたコンテキスト内の各Web要求を設定します。ドキュメントのSection 3.8 HTTP state management and execution contextをお読みください。また、this threadまで行ってください。

//create the local context to be shared across multiple requests of the same session 
HttpContext localContext = new BasicHttpContext(); 

// Create a local instance of cookie store 
CookieStore cookieStore = new BasicCookieStore(); 

// Bind custom cookie store to the local context 
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); 

// execute the post within the context 
HttpResponse response = client.execute(post,localContext); 

これで問題が解決しない場合は、wiresharkまたはFiddler2を使用してHTTP要求と応答トラフィックを検査してください。

関連する問題