簡體   English   中英

嘗試通過代理創建 HttpsURLConnection 時出錯

[英]Getting error while trying to create HttpsURLConnection through proxy

當我嘗試通過代理建立 url 連接時出現此錯誤

java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 405 Method Not Allowed"
    at sun.net.www.protocol.http.HttpURLConnection.doTunneling(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
    at com.test.sslpost.main(sslpost.java:81)

它在打開連接時產生錯誤,如果我嘗試不使用代理它工作正常。

請參閱下面描述的 java 代碼

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class sslpost {
    public static void main(String[] args) {
        try {
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }
            } };

            try {

                SSLContext sc = SSLContext.getInstance("SSL");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.setProperty("https.proxySet", "true");
            System.setProperty("https.proxyHost", "xxx.xxx.xxx.xxx");
            System.setProperty("https.proxyPort", "80");
            URL url = new URL("https://www.google.com");
            @SuppressWarnings("deprecation")
            HttpsURLConnection connection = (HttpsURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);

            connection.setRequestMethod("POST");
            connection.setFollowRedirects(true);

            String query = "serviceId="
                    + URLEncoder.encode("7");

            connection.setRequestProperty("Content-length",
                    String.valueOf(query.length()));
            connection.setRequestProperty("Content-Type",
                    "application/x-www- form-urlencoded");
            connection.setRequestProperty("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)");

            DataOutputStream output = new DataOutputStream(
                    connection.getOutputStream());

            output.writeBytes(query);

            System.out.println("Resp Code:" + connection.getResponseCode());
            System.out.println("Resp Message:"
                    + connection.getResponseMessage());

            DataInputStream input = new DataInputStream(
                    connection.getInputStream());

            for (int c = input.read(); c != -1; c = input.read())
                System.out.print((char) c);
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

HTTPS默認使用端口443,HTTP默認使用端口80。

在您的代碼中,您有這個

 System.setProperty("https.proxyPort", "80");

也許嘗試將其設置為443而不是80

打開C:\Users\yourUsername\.gradle ,您可能會找到一個包含代理設置的Gradle.build文件。 刪除它並重試,如果您沒有找到該文件,則可能是 inte.net 連接問題

我認為不需要更改代碼。 這意味着代理服務器無法隧道

您必須嘗試使用​​其他https代理 ...

systemProp.https.proxyHost=""

systemProp.https.proxyPort=""

systemProp.https.proxyPassword=""

systemProp.https.proxyUser=""  

您是否叫過“ super.doGet(req,resp);”? 在您的代碼中? 如下面?

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    super.doGet(req, resp);

我可以檢查super.doGet的實現,然后可以找到以下代碼。

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String protocol = req.getProtocol();
    String msg = lStrings.getString("http.method_get_not_supported");
    if (protocol.endsWith("1.1")) {
        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
    } else {
       resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM