在代理集不起作用的情况下使用Curl获取对Java的请求

时间:2019-02-21 17:53:48

标签: java curl https proxy

启用了代理的我的curl请求有效,但是我的Java代码不起作用。有任何想法可以帮助

卷曲代码(有效)

export https_proxy="test.com:80"
curl https://myurul.com

Java代码

    String url = "https://myurl.com";       
    URL obj = new URL(url);
    Properties sys = System.getProperties();
    sys.setProperty("https.proxyHost","test.com");
    sys.setProperty("https.proxyPort","443");
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //print result
    System.out.println(response.toString());

0 个答案:

没有答案