HttpClient HTTP / 1.1 302对象已移动

时间:2011-11-17 14:07:26

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

此代码适用于我的简单测试登录表单。它使用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();
            }
        }
    }

4 个答案:

答案 0 :(得分:2)

如果您使用的是httpclient 3x

GetMethod默认将followRedirects标志设置为true

您可以尝试为PostMethod

明确设置set redirect to 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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

答案一直是我发错了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"));