0

我有一个方法,它使用 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 会正确干扰。

提前致谢。

4

1 回答 1

0

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

例如,WebScarab 没有运行,或者您已将其配置为在与默认 8008 不同的端口上侦听,或者在与 localhost (127.0.0.1) 不同的接口上进行侦听。

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

于 2012-11-28T07:56:29.093 回答