httpclient重定向新手

时间:2011-09-25 16:25:03

标签: java httpclient

  

可能重复:
  Httpclient 4, error 302. How to redirect?

我想从我的comcast帐户中检索一些信息。使用本网站上的示例,我认为我非常接近。我正在使用firebug来查看要发布的内容,我发现当我登录时,我正被重定向。我不明白如何遵循重定向。我玩了无数的例子但是无法弄明白。我是编程的新手,只是没有运气这样做。这是我的代码。我进行初始登录,然后尝试转到另一个重定向开始的URL。一路上,我看到我正在获取大量的cookie,但不是重要的一个s_lst。

HttpPost httpPost = new HttpPost("https://login.comcast.net/login");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("continue", "https://login.comcast.net/account"));
        nvps.add(new BasicNameValuePair("deviceAuthn", "false"));
        nvps.add(new BasicNameValuePair("forceAuthn", "true"));
        nvps.add(new BasicNameValuePair("ipAddrAuthn", "false"));
        nvps.add(new BasicNameValuePair("lang", "en"));
        nvps.add(new BasicNameValuePair("passwd", "mypassword"));
        nvps.add(new BasicNameValuePair("r", "comcast.net"));
        nvps.add(new BasicNameValuePair("rm", "2"));
        nvps.add(new BasicNameValuePair("s", "ccentral-cima"));
        nvps.add(new BasicNameValuePair("user", "me"));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        System.out.println("executing request " + httpPost.getURI());
        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpPost, responseHandler);
        String cima = StringUtils.substringBetween(responseBody, "cima.ticket\" value=\"", "\">");
        System.out.println(cima);

        HttpPost httpPost2 = new HttpPost("https://customer.comcast.com/Secure/Home.aspx");

        List <NameValuePair> nvps2 = new ArrayList <NameValuePair>();
        nvps2.add(new BasicNameValuePair("cima.ticket", cima));

        httpPost2.setEntity(new UrlEncodedFormEntity(nvps2, HTTP.UTF_8));

        System.out.println("executing request " + httpPost2.getURI());
        // Create a response handler
        ResponseHandler<String> responseHandler2 = new BasicResponseHandler();
        String responseBody2 = httpclient.execute(httpPost2, responseHandler2);
        System.out.println(responseBody2);

1 个答案:

答案 0 :(得分:4)

以下是根据“响应处理”示例here改编的示例。

您的示例非常复杂 - 最好在您弄清楚如何遵循重定向时简化代码(您可以注释掉我突出显示的部分,以显示示例未能遵循重定向)。

import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.protocol.*;

public class ClientWithResponseHandler {

    public final static void main(String[] args) throws Exception {

        DefaultHttpClient httpclient = new DefaultHttpClient();
        // Comment out from here (Using /* and */)...
        httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {                
          public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)  {
            boolean isRedirect=false;
            try {
              isRedirect = super.isRedirected(request, response, context);
            } catch (ProtocolException e) {
              e.printStackTrace();
            }
            if (!isRedirect) {
              int responseCode = response.getStatusLine().getStatusCode();
              if (responseCode == 301 || responseCode == 302) {
                return true;
              }
            }
            return false;
          }
        });
        // ...to here and the request will fail with "HttpResponseException: Moved Permanently"
        try {
            HttpPost httpPost = new HttpPost("http://news.bbc.co.uk/");
            System.out.println("executing request " + httpPost.getURI());
            // Create a response handler
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpPost, responseHandler);
            System.out.println(responseBody);
            // Add your code here...
        } finally {
            // When HttpClient instance is no longer needed, shut down the connection 
            // manager to ensure immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }

}