3

在我向服务器发布帖子后,我需要帮助弄清楚如何获取重定向。首先,我需要从服务器获取一些 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);
4

4 回答 4

3

看我对这个问题的回答,

HttpClient 4 - 如何捕获最后一个重定向 URL

于 2010-01-09T16:00:38.187 回答
1

我假设您想要自动化浏览器操作并维护会话,以便您也可以访问那些需要维护会话的页面。

我不知道如何通过 org.apache.http.client API 做到这一点。如果您不限于使用 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 回答
1

我在 Java 中有一个简单的方法。以下是步骤:

  1. 创建 HttpUrl 连接。
  2. 设置HttpURLConnection.setFollowRedirects( true );// 这应该默认为真
  3. 通过您的 HttpUrlConnection 对象调用连接;
  4. 连接后调用getHeadersFields()你的 HttpUrlConnection 对象;
  5. getUrl()通过调用上面的 HttpUrlConnection 对象获取重定向的 URL ;

还有另一种使用 HTTP Headers 中的 Location 字段获取它的方法,但有时我们不会在 headers 中获取 Location 字段。至少它对我不起作用。但是上面的方法,它确实有效。

于 2010-09-07T11:22:54.853 回答
0

它在location标题中可用。

于 2010-01-09T14:08:21.690 回答