我有一个方法,它使用Apache httpComponents HttpClient类通过Webscarab代理连接到网页。我从Apache Software Foundation here获得了这个方法。下面是我的hole方法:
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();
}
}HttpResponse rsp = httpclient.execute(target, req);之前的println,
返回这个: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)当我尝试从其他浏览器访问页面时,Webscarab会正确干扰,比如火狐、Chrome或JavaFX webengine。
提前谢谢。
发布于 2012-11-28 15:56:29
由于连接被拒绝,我只能假设WebScarab没有侦听您尝试连接的端口(在您尝试连接的时候)。
例如,WebScarab没有运行,或者您已将其配置为在默认8008以外的端口上侦听,或者在本地主机(127.0.0.1)的不同接口上侦听。
你的代码和WebScarab运行在同一台机器上吗?如果不是这样,您需要让WebScarab侦听可通过网络访问的实际网络接口,并更新您的代码以引用该IP地址而不是本地主机。
https://stackoverflow.com/questions/13595405
复制相似问题