日期:2014-05-20  浏览次数:20875 次

httpclient4.1.2做proxy里遇到的一个问题

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * How to send a request via proxy using {@link HttpClient}.
 *
 * @since 4.0
 */
public class ProxyTest {

  public static void main(String[] args)throws Exception {
  HttpHost proxy = new HttpHost("194.170.16.75", 80, "http");

  DefaultHttpClient httpclient = new DefaultHttpClient();
  try {
  httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

  HttpHost target = new HttpHost("www.ip138.com/ip2city.asp", 80, "http"); HttpGet req = new HttpGet("/");

  System.out.println("executing request to " + target + " via " + proxy);
  HttpResponse rsp = httpclient.execute(target, req);
  HttpEntity entity = rsp.getEntity();

  System.out.println("----------------------------------------");
  System.out.println(rsp.getStatusLine());
  Header[] headers = rsp.getAllHeaders();
  for (int i = 0; i<headers.length; i++) {
  System.out.println(headers[i]);
  }
  System.out.println("----------------------------------------");

  if (entity != null) {
  System.out.println(EntityUtils.toString(entity));
  }

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

}


主要使用的是HttpHost这个类,但遇到了这样一个问题,如果直接使用域名的话(如,www.ip138.net)测试一切正常,如果换成具体到哪个页面(如,www.ip138.net/ip2city.asp)就会报错
查阅了这类的具体方法如下:
// Constructors
  public HttpHost(String hostname, int port, String scheme) { }
  public HttpHost(String hostname, int port) { }
  public HttpHost(String hostname) { }
  public HttpHost(HttpHost httphost) { }
于是改成了第三种形参只带域名测试正常,再加上具体的页面又访问不到了
怀疑应该是不管你是否参数带上端口,默认都帮你加上了端口如,www.ip138.net:80,这个是可以访问到网页的,但如果是www.ip138.net/ip2city.asp:80,这就会报错了。
这个问题需要如何解决?

------解决方案--------------------
具体页面应该在HttpGet里面进行获得