HttpClient HTTP/1.1 302 对象已移动

发布于 2024-12-16 16:20:59 字数 2562 浏览 2 评论 0原文

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

This code works for my simple testing login form. It uses POST to log in and then print all the information to the screen from logged view. But it does not work on one specific website I have been creating this code all the way. Any ideas why this is happening and how to fix it?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

写下不归期 2024-12-23 16:20:59

如果您使用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 了解更多详细信息

If you are using httpclient 3x

GetMethod has followRedirects flag set to true by default

You could try set set redirect to true explicitly for PostMethod

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

If you are using httpcomponents

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

See http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html#d4e1192 for further details

半岛未凉 2024-12-23 16:20:59

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

302 means that the page has moved. You need to check the Location header on the response and retry your request on the url in that header.

梦年海沫深 2024-12-23 16:20:59

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

HTTP code 302 is one way to make a redirect. Your code probably correctly performs the login and then the server redirects it to another page. Either way, you should see the Location: header in the response and follow it.

我们的影子 2024-12-23 16:20:59

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

The answer all along was that I was sending wrong POST queries. Instead of:

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

Had to write:

nvps.add(new BasicNameValuePair("login", "nnnnick"));
nvps.add(new BasicNameValuePair("pw", "pppassswww123"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文