HttpClient HTTP/1.1 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();
}
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用httpclient 3x
GetMethod 将 followRedirects 标志设置为 true默认的
你可以试试将 PostMethod 的 set redirect 显式设置为 true
如果您使用 httpcomponents
请参阅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
If you are using httpcomponents
See http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html#d4e1192 for further details
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.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.一直以来的答案是我发送了错误的 POST 查询。而不是:
必须写:
The answer all along was that I was sending wrong POST queries. Instead of:
Had to write: