各位高手,我第一次使用HttpClient登陆淘宝并提交查询商品表单,我建好了连接用post方法发了请求过去,我希望得到服务器返回查询后页面的URL,服务器返回了Http 1.1 302 的重定向信息回来,接着程序对其进行判断并重新捉取信息,但还是不能得到查询后页面的URL
希望各位高手指导一下小弟
下面是我的代码:
import java.io.IOException;
import org.apache.commons.httpclient.*;import org.apache.commons.httpclient.methods.*;
public class SimpleHttpClient {
public static void main(String[] args) throws IOException { HttpClient client = new HttpClient(); client.getHostConfiguration().setHost("www.taobao.com", 80, "http");
HttpMethod method = getPostMethod();//使用POST方式提交数据 int statusCode = client.executeMethod(method);
if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY){
Header locationHeader = method.getRequestHeader("location");
String location = null;
if(locationHeader != null )
{
location = locationHeader.getValue();
System.out.println("The Page was redirected to: " + location);
}
else
{
System.err.println("Location field value is null ");
}
return;
}
//打印服务器返回的状态 System.out.println(method.getStatusLine()); //打印结果页面 String response = new String(method.getResponseBodyAsString().getBytes("8859_1")); //打印返回的信息 System.out.println(response); method.releaseConnection(); }
/** * 使用POST方式提交数据 * @return */ private static HttpMethod getPostMethod(){ PostMethod post = new PostMethod("http://search1.taobao.com/browse/search_auction.htm?at_topsearch=1"); NameValuePair q = new NameValuePair("q","N73"); post.setRequestBody(new NameValuePair[] { q});
return post; }}
希望各位高手指导一下小弟
下面是我的代码:
import java.io.IOException;
import org.apache.commons.httpclient.*;import org.apache.commons.httpclient.methods.*;
public class SimpleHttpClient {
public static void main(String[] args) throws IOException { HttpClient client = new HttpClient(); client.getHostConfiguration().setHost("www.taobao.com", 80, "http");
HttpMethod method = getPostMethod();//使用POST方式提交数据 int statusCode = client.executeMethod(method);
if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY){
Header locationHeader = method.getRequestHeader("location");
String location = null;
if(locationHeader != null )
{
location = locationHeader.getValue();
System.out.println("The Page was redirected to: " + location);
}
else
{
System.err.println("Location field value is null ");
}
return;
}
//打印服务器返回的状态 System.out.println(method.getStatusLine()); //打印结果页面 String response = new String(method.getResponseBodyAsString().getBytes("8859_1")); //打印返回的信息 System.out.println(response); method.releaseConnection(); }
/** * 使用POST方式提交数据 * @return */ private static HttpMethod getPostMethod(){ PostMethod post = new PostMethod("http://search1.taobao.com/browse/search_auction.htm?at_topsearch=1"); NameValuePair q = new NameValuePair("q","N73"); post.setRequestBody(new NameValuePair[] { q});
return post; }}
这个可以取到的,没有取到有可能是服务器那边没有给你设置