【问题标题】:HttpClient HTTP/1.1 302 Object movedHttpClient HTTP/1.1 302 对象已移动
【发布时间】:2011-11-17 14:07:25
【问题描述】:

此代码适用于我的简单测试登录表单。它使用 POST 登录,然后将所有信息从日志视图中打印到屏幕上。但它不适用于我一直在创建此代码的特定网站。任何想法为什么会发生这种情况以及如何解决它?

package visualutopiabot;

import org.apache.http.client.ResponseHandler;
import org.apache.http.impl.client.BasicResponseHandler;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

    public class Main {

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

            DefaultHttpClient httpclient = new DefaultHttpClient();
            try {

                /* POST login */
                HttpPost httpost = new HttpPost("http://website.com/login.asp");

                List <NameValuePair> nvps = new ArrayList <NameValuePair>();
                nvps.add(new BasicNameValuePair("username", "nnnnick"));
                nvps.add(new BasicNameValuePair("password", "pppassswww123"));

                httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
                HttpResponse response = httpclient.execute(httpost);
                HttpEntity entity = response.getEntity();
                System.out.println("Login form get: " + response.getStatusLine());
                EntityUtils.consume(entity);

                /* get content*/
                HttpGet httpget = new HttpGet("http://website.com/index.asp");

                System.out.println("executing request " + httpget.getURI());

                // Create a response handler
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String responseBody = httpclient.execute(httpget, responseHandler);
                System.out.println("----------------------------------------");
                System.out.println(responseBody);
                System.out.println("----------------------------------------");


            } finally {
                // When HttpClient instance is no longer needed,
                // shut down the connection manager to ensure
                // immediate deallocation of all system resources
                httpclient.getConnectionManager().shutdown();
            }
        }
    }

【问题讨论】:

    标签: java httpclient http-status-code-302


    【解决方案1】:

    如果您使用的是httpclient 3x

    GetMethod 默认情况下将 followRedirects 标志设置为 true

    您可以尝试为 PostMethod 显式设置 set redirect 为 true

     PostMethod postMethod = ...;
     postMethod.setFollowRedirects(true)
    

    如果您使用的是httpcomponents

    httpclient.setRedirectStrategy(new DefaultRedirectStrategy());
    httpost.getParams().setParameter("http.protocol.handle-redirects",true);
    

    详情请见http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html#d4e1192

    【讨论】:

      【解决方案2】:

      302 表示页面已移动。您需要检查响应中的 Location 标头,然后在该标头中的 url 上重试您的请求。

      【讨论】:

        【解决方案3】:

        HTTP 代码 302 是进行重定向的一种方式。您的代码可能正确执行登录,然后服务器将其重定向到另一个页面。无论哪种方式,您都应该在响应中看到 Location: 标头并关注它。

        【讨论】:

        • 是的,位置设置为另一个。所以假设标题显示新位置:YYY.com 我更改了这部分:HttpPost httpost = new HttpPost("YYY.com");但它仍然无法正常工作。
        • 您不应更改原始登录请求,而应在登录回复重定向后添加另一个请求。
        • 哦,我想我找到了。忘记将用户名和密码更改为站点在此处使用的其他字段:nvps.add(new BasicNameValuePair("username", "nnnnick")); nvps.add(new BasicNameValuePair("password", "pppassswww123"));这个错误花了很多时间。
        【解决方案4】:

        答案一直是我发送了错误的 POST 查询。而不是:

        nvps.add(new BasicNameValuePair("username", "nnnnick"));
        nvps.add(new BasicNameValuePair("password", "pppassswww123"));
        

        必须写:

        nvps.add(new BasicNameValuePair("login", "nnnnick"));
        nvps.add(new BasicNameValuePair("pw", "pppassswww123"));
        

        【讨论】:

          猜你喜欢
          • 2017-02-27
          • 1970-01-01
          • 1970-01-01
          • 2010-11-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-21
          相关资源
          最近更新 更多