3

サーバーに投稿した後、リダイレクトを取得する方法を理解するのに助けが必要です。まず、サーバーからCookieを取得するためにgetを実行する必要があります。次に、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);
4

4 に答える 4

3

この質問に対する私の答えを見てください、

HttpClient4-最後のリダイレクトURLをキャプチャする方法

于 2010-01-09T16:00:38.187 に答える
1

私がJavaで思いついた簡単な方法があります。手順は次のとおりです。

  1. HttpUrl接続を作成します。
  2. 設定HttpURLConnection.setFollowRedirects( true );//これはデフォルトでtrueである必要があります
  3. HttpUrlConnectionオブジェクトを介してconnectを呼び出します。
  4. 接続後getHeadersFields()、HttpUrlConnectionオブジェクトを介して呼び出します。
  5. getUrl()上記のHttpUrlConnectionオブジェクトを呼び出して、リダイレクトされたURLを取得します。

HTTPヘッダーのLocationフィールドを使用して取得する別の方法もありますが、ヘッダーのLocationフィールドを取得しない場合があります。少なくとも私にはうまくいきませんでした。しかし、上記の方法は確かに機能します。

于 2010-09-07T11:22:54.853 に答える
1

ブラウザの操作を自動化し、セッションを維持して、セッションを維持する必要のあるページにもアクセスできるようにすることを想定しています。

org.apache.http.clientAPIを使用してこれを行う方法がわかりません。org.apache.http.client APIの使用に制限されておらず、他のAPIを使用できる場合は、HtmlUnit 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

上記のコードがどのようにブラウザの操作を自動化し、どのようにセッションを自動的に維持するかがわかります。CookieやURLReDirectを手動で処理する必要はありません...

于 2010-01-09T15:04:45.977 に答える
0

locationヘッダーで利用できます。

于 2010-01-09T14:08:21.690 に答える