2

当我尝试通过代理进行 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();
        }
    }
}
4

4 回答 4

1

你有没有调用“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);
    }
}
于 2013-05-17T08:55:20.993 回答
1

HTTPS 默认使用端口 443,而 HTTP 默认使用端口 80。

在您的代码中,您有这个

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

也许尝试将其设置为 443 而不是 80

于 2012-04-22T06:24:08.667 回答
0

我认为不需要更改代码。这意味着代理服务器无法建立隧道

您必须尝试使用​​不同的https 代理...

systemProp.https.proxyHost=""

systemProp.https.proxyPort=""

systemProp.https.proxyPassword=""

systemProp.https.proxyUser=""  
于 2012-04-22T06:52:01.387 回答
0

打开C:\Users\yourUsername\.gradle,您可能会发现一个Gradle.build包含您的代理设置的文件。删除后重试,如果找不到文件,可能是网络连接问题

于 2021-08-15T17:55:31.363 回答