使用 Java org.apache.http.client 获取重定向 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2033577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:02:35  来源:igfitidea点击:

Get hold of redirect url with Java org.apache.http.client

javapostredirecthttpclient

提问by jakob

I need help with figuring out how to get hold of the redirect after I make a post to the server. First, I need to do a get to obtain some cookies from the server. Then I perform a post with the cookies and additional parameters. The server then answers with a 302 redirect. How do I get the url for that redirect?

在我向服务器发帖后,我需要帮助弄清楚如何控制重定向。首先,我需要做一个 get 从服务器获取一些 cookie。然后我使用 cookie 和其他参数执行帖子。然后服务器以 302 重定向进行响应。如何获取该重定向的 url?

Code looks like follows:

代码如下所示:

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);

采纳答案by ZZ Coder

See my answer to this question,

看我对这个问题的回答,

HttpClient 4 - how to capture last redirect URL

HttpClient 4 - 如何捕获上次重定向 URL

回答by Abhay Dandekar

There is a simple way I came about in Java. Following are the steps:

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

  1. Create the HttpUrl connection.
  2. Set HttpURLConnection.setFollowRedirects( true );// this should be true by default
  3. Call connect over your HttpUrlConnection object;
  4. After connect call the getHeadersFields()over your HttpUrlConnection object;
  5. Get the redirected URL by calling the getUrl()over the above HttpUrlConnection object;
  1. 创建 HttpUrl 连接。
  2. 设置HttpURLConnection.setFollowRedirects( true );// 默认情况下这应该是真的
  3. 通过您的 HttpUrlConnection 对象调用连接;
  4. 连接后调用getHeadersFields()你的 HttpUrlConnection 对象;
  5. 通过调用getUrl()上面的HttpUrlConnection对象来获取重定向的URL ;

There is also another way of getting it using the Location field in HTTP Headers, but sometimes we do not get the Location field in headers. It did not work for me at least. But the above method, it works for sure.

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

回答by Yatendra Goel

I am assuming that you want to automate browser operations and maintain session so that you can access those pages too which need session to be maintained.

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

I don't know how to this through org.apache.http.client API. If you are not restricted to use org.apache.http.client API and can use other API then you can use HtmlUnitAPI otherwise you can ignore the rest of the answer.

我不知道如何通过 org.apache.http.client API 做到这一点。如果您不限于使用 org.apache.http.client API 并且可以使用其他 API,那么您可以使用HtmlUnitAPI,否则您可以忽略其余的答案。

Maintaining sessions and automating browser operations through HtmlUnitcan be done as follows:

通过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

As you can see that how the above code automate browser operations and how it maintains session automatically. We don't need to handle cookies or URLReDirect manually...

如您所见,上面的代码如何自动化浏览器操作以及如何自动维护会话。我们不需要手动处理 cookie 或 URLReDirect...

回答by BalusC

It's available in the locationheader.

它在location标题中可用。