首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache httpComponents HttpClient 4.2.2无法通过Webscarab代理连接到网页

Apache httpComponents HttpClient 4.2.2无法通过Webscarab代理连接到网页
EN

Stack Overflow用户
提问于 2012-11-28 07:56:22
回答 1查看 1.1K关注 0票数 0

我有一个方法,它使用Apache httpComponents HttpClient类通过Webscarab代理连接到网页。我从Apache Software Foundation here获得了这个方法。下面是我的hole方法:

代码语言:javascript
复制
       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

那么下面的异常就会发生。

代码语言:javascript
复制
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。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2012-11-28 15:56:29

由于连接被拒绝,我只能假设WebScarab没有侦听您尝试连接的端口(在您尝试连接的时候)。

例如,WebScarab没有运行,或者您已将其配置为在默认8008以外的端口上侦听,或者在本地主机(127.0.0.1)的不同接口上侦听。

你的代码和WebScarab运行在同一台机器上吗?如果不是这样,您需要让WebScarab侦听可通过网络访问的实际网络接口,并更新您的代码以引用该IP地址而不是本地主机。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13595405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档