0

Use lantern VPN, the chrome browser and eclipse have the same proxy and URL could get response but java code gets connection timed out.

The lantern VPN proxy: the lanter vpn proxy

Eclipse proxy: eclipse proxy

The result for the url http://api.zb.com/data/v1/markets: the result for the url

The code for get response:

String callback= "";
    try {
        // request url
        String url = ZBConfig.API_DATA + "/markets";
        log.info("markets configuration url: " + url);
        // request call back
        callback = HttpUtilManager.get(url, "UTF-8");
        log.info("-markets configuration url:: " + callback);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    public static String get(String urlAll, String charset) {
    BufferedReader reader = null;
    String result = null;
    StringBuffer sbf = new StringBuffer();
    String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";// 模拟浏览器
    try {
        URL url = new URL(urlAll);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setReadTimeout(30000);
        connection.setConnectTimeout(30000);
        connection.setRequestProperty("User-agent", userAgent);
        connection.connect();
        InputStream is = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(is, charset));
        String strRead = null;
        while ((strRead = reader.readLine()) != null) {
            sbf.append(strRead);
            sbf.append("\r\n");
        }
        reader.close();
        result = sbf.toString();

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

The error message from the console:

12:18:44.508 [main] INFO com.Test - markets configuration url: http://api.zb.com/data/v1/markets
java.net.ConnectException: Connection timed out: connect
12:19:06.095 [main] INFO com.Test - -markets configuration url:: null
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
    at sun.net.www.http.HttpClient.New(HttpClient.java:339)
    at sun.net.www.http.HttpClient.New(HttpClient.java:357)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984)
    at com.zb.kits.HttpUtilManager.get(HttpUtilManager.java:353)
    at com.Test.main(Test.java:201)
Rohan Pillai
  • 917
  • 3
  • 17
  • 26
richard_sun
  • 71
  • 1
  • 4

1 Answers1

0

I had the same scenario in which I intended to bypass filtering which was applied on Youtube and instead of Lantern, I used to use Psiphon. After running Psiphon it exposes a local port which you can use as proxy(and I'm sure Lantern is the same just check its log) and since it is applied as a system level proxy chrome can use it without any config, but if you wanna use it with Firefox you have to config it in the settings, and in order to use any proxy in your java codes you have to config jvm to use that proxy.

This is the sample code I used:

System.getProperties().put("http.proxyHost", Config.getHTTPPROXYHOST());
System.getProperties().put("http.proxyPort", Config.getHTTPPROXYPORT());
System.getProperties().put("https.proxyHost", Config.getHTTPSPROXYHOST());
System.getProperties().put("https.proxyPort", Config.getHTTPSPROXYPORT());

Also you can check the code in my github, in addition this post in stackoverflow can be quite useful.

hope it helps.

ali4j
  • 522
  • 3
  • 15
  • I have tried your suggestion, before sending request set proxy code here, but didn't work for me. System.getProperties().setProperty("http.proxyHost", "127.0.0.1"); System.getProperties().setProperty("http.proxyPort", "55677"); – richard_sun Apr 15 '18 at 06:08
  • use those values for https properties too. I mean "https.proxyHost" and "https.proxyPort" – ali4j Apr 15 '18 at 06:36