如何使用Apache HttpClient使用代理在Java中加载URL?

时间:2014-03-07 06:13:24

标签: java http url httpclient ping

您好我正在尝试使用Apache HttpClient在JAVA中使用服务器的URL来测试响应时间

这是我的代码

    HttpClient client = new HttpClient();
    HttpMethod method = new HeadMethod("http://www.google.com/");
    StopWatch sWatch = new StopWatch();

    try {

        sWatch.start();
    client.executeMethod(method);

    System.out.println(sWatch.toString());

    }catch(Exception e)
    {
        e.printStackTrace();
    }

我的问题是我可以从我的浏览器访问www.google.com但是当我尝试执行该逻辑时会给出此异常

    java.net.ConnectException: Connection timed out: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:280)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at com.sampl.DAP.CheckResponse.checkStatus(CheckResponse.java:31)
at com.sampl.DAP.CheckResponse.main(CheckResponse.java:44)

2 个答案:

答案 0 :(得分:1)

尝试以下

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author djc39_000
 */
public class URLBrowser {

    public static void main(String[] args) {
        try {            
            URLConnection uc =  new URL("https://www.google.com").openConnection(
                    new Proxy(Proxy.Type.HTTP, new InetSocketAddress("my.proxy.example.com", 3128))
            );
            BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
            String line;            
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch (MalformedURLException ex) {
            Logger.getLogger(URLBrowser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(URLBrowser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

我以这种方式编写程序,这样如果你没有代理,你可以注释掉那一行,其他一切仍然有用。如果你使用var代理代理=新代理(...),那么你必须注释多行。

答案 1 :(得分:1)

因为您正在使用库。我建议使用jsoup。以下代码已经过测试。

long startTime = System.nanoTime();
Response response= Jsoup.connect(location)
       .ignoreContentType(true)
       .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")  
       .referrer("http://www.google.com")   
       .timeout(12000) 
       .followRedirects(true)
       .execute();
long endTime = System.nanoTime();