我有一个方法,它使用 Apache httpComponents HttpClient 类通过 Webscarab 代理连接到网页。我在这里从 Apache Software Foundation 获得方法 。下面是我的打洞方法:
public void HTTPGet(){
//HttpHost proxy = new HttpHost("localhost", 8008);
System.setProperty("http.proxyHost", "localhost");
System.setProperty("http.proxyPort", "8008");
HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost", "localhost"),Integer.parseInt(System.getProperty("http.proxyPort", "8008")));
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy);
HttpHost target = new HttpHost("www.google.gr/", 80);
HttpGet req = new HttpGet("/");
System.out.println("executing request to " + target + " via "
+ proxy);
HttpResponse rsp = httpclient.execute(target, req); //here I get the exception as below
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));
}
} catch (IOException ex) {
Logger.getLogger(WebBrowser.class.getName()).log(Level.SEVERE, null, ex);
}
finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
之前的 println HttpResponse rsp = httpclient.execute(target, req);
,
返回这个:executing request to http://www.google.gr/:80 via http://localhost:8008
然后发生以下异常。
Nov 28, 2012 1:23:09 AM student.WebBrowser HTTPGet
SEVERE: null
org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8008 refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:827)
当我尝试从其他浏览器(如 Firefox、Chrome 或 JavaFX webengine)访问页面时,Webcarab 会正确干扰。
提前致谢。