Java org.apache.http.clientでリダイレクトURLを取得する
-
19-09-2019 - |
質問
サーバーに投稿した後にリダイレクトを取得する方法を理解するために助けが必要です。まず、get を実行してサーバーから Cookie を取得する必要があります。次に、Cookie と追加のパラメーターを使用して投稿を実行します。その後、サーバーは 302 リダイレクトで応答します。そのリダイレクトの URL を取得するにはどうすればよいですか?
コードは次のようになります。
HttpGet get = new HttpGet(urlOne);
try {
//Creating a local instance of cookie store.
CookieStore cookieJar = new BasicCookieStore();
// Creating a local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar);
HttpResponse response = httpClient.execute(get, localContext);
HttpEntity entity = response.getEntity();
System.out.println("------------------GET----------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
// Print out cookies obtained from server
List<Cookie> cookies = cookieJar.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
if (entity != null) {
entity.consumeContent();
}
System.out.println("------------------GET-END---------------------");
// Create a new post
HttpPost post = new HttpPost(urlTwo);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
// Add params
HttpParams params = new BasicHttpParams();
params.setParameter("action", "search");
params.setParameter("word", "hello");
post.setParams(params);
//Execute
HttpResponse response2 = httpClient.execute(post, localContext);
解決
タグ、この質問に対する私の答えを参照してください。
他のヒント
私はあなたのブラウザ操作を自動化し、維持するために、セッションを必要とあまりにもこれらのページにアクセスできるようにセッションを維持することを想定しています。
私はどのようにこれまでorg.apache.http.client APIを介して知りません。あなたがorg.apache.http.client APIを使用するように制限されず、他のAPIを使用することができるなら、あなたは HtmlUnit 使用することができますA> APIそうあなたは答えの残りの部分を無視することができます。
次のようにセッションを維持し、 HtmlUnit を介してブラウザ操作を自動化することは行うことができます:
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
final WebClient webClient = new WebClient();
try {
webClient.setJavaScriptEnabled(true);
webClient.setThrowExceptionOnScriptError(false);
webClient.setCssEnabled(true);
webClient.setUseInsecureSSL(true);
webClient.setRedirectEnabled(true);
HtmlPage loginPage = webClient.getPage(new URL("https://www.orkut.com/"));
System.out.println(loginPage.getTitleText());
List<HtmlForm> forms = loginPage.getForms();
HtmlForm loginForm = forms.get(0);
HtmlTextInput username = loginForm.getInputByName("Email");
HtmlPasswordInput password = loginForm.getInputByName("Passwd");
HtmlInput submit = loginForm.getInputByName("signIn");
username.setValueAttribute("username");
password.setValueAttribute("password");
HtmlPage homePage = submit.click();.
Thread.sleep(10 * 1000);
HtmlPage homePageFrame = (HtmlPage) homePage.getFrameByName("orkutFrame").getEnclosedPage();
HtmlPage communitiesTestPage = (HtmlPage) webClient.openWindow(new URL("http://www.orkut.co.in/Main#Community?cmm=1"), "CommunitiesWindow").getEnclosedPage();
}catch(java.security.GeneralSecurityException e) {
e.printStackTrace();
}catch(java.io.IOException e) {
e.printStackTrace();
}catch(InterruptedException e) {
e.printStackTrace();
}
WebWindow ww = webClient.getWebWindowByName("CommunitiesWindow");
WebRequestSettings wrs1 = new WebRequestSettings(URL); // URL is the url that requires authentication first
あなたはどのように上記のコードの自動化ブラウザ操作とどのようにそれが自動的にセッションを維持していることを見ることができるように。私たちはクッキーを扱うか、する必要はありませんURLReDirect手動...
Java で私が思いついた簡単な方法があります。手順は次のとおりです。
- HttpUrl 接続を作成します。
- セット
HttpURLConnection.setFollowRedirects( true );
// これはデフォルトで true になるはずです - HttpUrlConnection オブジェクトを介して connect を呼び出します。
- 接続後、
getHeadersFields()
HttpUrlConnection オブジェクト上で。 - を呼び出してリダイレクトされた URL を取得します。
getUrl()
上記の HttpUrlConnection オブジェクト上で。
HTTP ヘッダーの Location フィールドを使用して取得する別の方法もありますが、ヘッダーの Location フィールドを取得できない場合があります。少なくとも私には効果がありませんでした。しかし、上記の方法は確実に機能します。
これは location
のヘッダで利用可能です。