{"id":112,"date":"2023-02-18T10:03:36","date_gmt":"2023-02-18T02:03:36","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=112"},"modified":"2023-02-18T10:03:52","modified_gmt":"2023-02-18T02:03:52","slug":"java-proxy-http-https","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/java-proxy-http-https\/","title":{"rendered":"Java\u8bbe\u7f6e\u4ee3\u7406\u7684\u4e24\u79cd\u65b9\u5f0f\uff08HTTP\u548cHTTPS\uff09"},"content":{"rendered":"<h3>\u76f4\u63a5\u8bbe\u7f6e\u7cfb\u7edf\u5c5e\u6027\uff0c\u8bbe\u7f6e\u540e\u6240\u6709\u7f51\u7edc\u8bf7\u6c42\u90fd\u6709\u6548<\/h3>\n<pre><code class=\"language-java\">System.setProperty(&quot;proxyType&quot;, &quot;4&quot;);\nSystem.setProperty(&quot;proxyPort&quot;, &quot;80&quot;));\nSystem.setProperty(&quot;proxyHost&quot;, &quot;127.0.0.1&quot;);\nSystem.setProperty(&quot;proxySet&quot;, &quot;true&quot;);<\/code><\/pre>\n<p><!-- more --><\/p>\n<h3>\u4f7f\u7528<code>java.net.Proxy<\/code>\u7c7b<\/h3>\n<pre><code class=\"language-java\">import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.io.PrintWriter;\nimport java.net.HttpURLConnection;\nimport java.net.InetSocketAddress;\nimport java.net.Proxy;\nimport java.net.URL;\nimport java.net.Proxy.Type;\nimport java.security.cert.CertificateException;\nimport java.security.cert.X509Certificate;\n\nimport javax.net.ssl.HostnameVerifier;\nimport javax.net.ssl.HttpsURLConnection;\nimport javax.net.ssl.SSLContext;\nimport javax.net.ssl.SSLSession;\nimport javax.net.ssl.TrustManager;\nimport javax.net.ssl.X509TrustManager;\n\npublic class HttpAndHttpsProxy {\n\n    public static String HttpsProxy(String url, String param, String proxy, int port) {\n        HttpsURLConnection httpsConn = null;\n        PrintWriter out = null;\n        BufferedReader in = null;\n        String result = &quot;&quot;;\n        BufferedReader reader = null;\n        try {\n            URL urlClient = new URL(url);\n            System.out.println(&quot;\u8bf7\u6c42\u7684URL========\uff1a&quot; + urlClient);\n\n                SSLContext sc = SSLContext.getInstance(&quot;SSL&quot;);\n                \/\/ \u6307\u5b9a\u4fe1\u4efbhttps\n                sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());\n                \/\/\u521b\u5efa\u4ee3\u7406\u867d\u7136\u662fhttps\u4e5f\u662fType.HTTP\n                Proxy proxy1=new Proxy(Type.HTTP, new InetSocketAddress(proxy, port));\n                \/\/\u8bbe\u7f6e\u4ee3\u7406\n                httpsConn = (HttpsURLConnection) urlClient.openConnection(proxy1);\n\n                httpsConn.setSSLSocketFactory(sc.getSocketFactory());\n                httpsConn.setHostnameVerifier(new TrustAnyHostnameVerifier());\n                 \/\/ \u8bbe\u7f6e\u901a\u7528\u7684\u8bf7\u6c42\u5c5e\u6027\n                httpsConn.setRequestProperty(&quot;accept&quot;, &quot;*\/*&quot;);\n                httpsConn.setRequestProperty(&quot;connection&quot;, &quot;Keep-Alive&quot;);\n                httpsConn.setRequestProperty(&quot;user-agent&quot;,\n                        &quot;Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)&quot;);\n                \/\/ \u53d1\u9001POST\u8bf7\u6c42\u5fc5\u987b\u8bbe\u7f6e\u5982\u4e0b\u4e24\u884c\n                httpsConn.setDoOutput(true);\n                httpsConn.setDoInput(true);\n                \/\/ \u83b7\u53d6URLConnection\u5bf9\u8c61\u5bf9\u5e94\u7684\u8f93\u51fa\u6d41\n                out = new PrintWriter(httpsConn.getOutputStream());\n                \/\/ \u53d1\u9001\u8bf7\u6c42\u53c2\u6570\n                out.print(param);\n                \/\/ flush\u8f93\u51fa\u6d41\u7684\u7f13\u51b2\n                out.flush();\n                \/\/ \u5b9a\u4e49BufferedReader\u8f93\u5165\u6d41\u6765\u8bfb\u53d6URL\u7684\u54cd\u5e94\n                in = new BufferedReader(\n                        new InputStreamReader(httpsConn.getInputStream()));\n                String line;\n                while ((line = in.readLine()) != null) {\n                    result += line;\n                }\n                \/\/ \u65ad\u5f00\u8fde\u63a5\n                httpsConn.disconnect();\n                System.out.println(&quot;====result====&quot; + result);\n                System.out.println(&quot;\u8fd4\u56de\u7ed3\u679c\uff1a&quot; + httpsConn.getResponseMessage());\n\n        } catch (Exception e) {\n            e.printStackTrace();\n        } finally {\n            try {\n                if (reader != null) {\n                    reader.close();\n                }\n            } catch (IOException e) {\n            }\n            try {\n                if (in != null) {\n                    in.close();\n                }\n            } catch (IOException e) {\n                e.printStackTrace();\n            }\n            if (out != null) {\n                out.close();\n            }\n        }\n\n         return result;\n    }\n\n    public static String HttpProxy(String url, String param, String proxy, int port) {\n        HttpURLConnection httpConn = null;\n        PrintWriter out = null;\n        BufferedReader in = null;\n        String result = &quot;&quot;;\n        BufferedReader reader = null;\n        try {\n            URL urlClient = new URL(url);\n            System.out.println(&quot;\u8bf7\u6c42\u7684URL========\uff1a&quot; + urlClient);\n                \/\/\u521b\u5efa\u4ee3\u7406\n                Proxy proxy1=new Proxy(Type.HTTP, new InetSocketAddress(proxy, port));\n                \/\/\u8bbe\u7f6e\u4ee3\u7406\n                httpConn = (HttpURLConnection) urlClient.openConnection(proxy1);\n                \/\/ \u8bbe\u7f6e\u901a\u7528\u7684\u8bf7\u6c42\u5c5e\u6027\n                httpConn.setRequestProperty(&quot;accept&quot;, &quot;*\/*&quot;);\n                httpConn.setRequestProperty(&quot;connection&quot;, &quot;Keep-Alive&quot;);\n                httpConn.setRequestProperty(&quot;user-agent&quot;,\n                        &quot;Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)&quot;);\n                \/\/ \u53d1\u9001POST\u8bf7\u6c42\u5fc5\u987b\u8bbe\u7f6e\u5982\u4e0b\u4e24\u884c\n                httpConn.setDoOutput(true);\n                httpConn.setDoInput(true);\n                \/\/ \u83b7\u53d6URLConnection\u5bf9\u8c61\u5bf9\u5e94\u7684\u8f93\u51fa\u6d41\n                out = new PrintWriter(httpConn.getOutputStream());\n                \/\/ \u53d1\u9001\u8bf7\u6c42\u53c2\u6570\n                out.print(param);\n                \/\/ flush\u8f93\u51fa\u6d41\u7684\u7f13\u51b2\n                out.flush();\n                \/\/ \u5b9a\u4e49BufferedReader\u8f93\u5165\u6d41\u6765\u8bfb\u53d6URL\u7684\u54cd\u5e94\n                in = new BufferedReader(\n                        new InputStreamReader(httpConn.getInputStream()));\n                String line;\n                while ((line = in.readLine()) != null) {\n                    result += line;\n                }\n                \/\/ \u65ad\u5f00\u8fde\u63a5\n                httpConn.disconnect();\n                System.out.println(&quot;====result====&quot; + result);\n                System.out.println(&quot;\u8fd4\u56de\u7ed3\u679c\uff1a&quot; + httpConn.getResponseMessage());\n        } catch (Exception e) {\n            e.printStackTrace();\n        } finally {\n            try {\n                if (reader != null) {\n                    reader.close();\n                }\n            } catch (IOException e) {\n            }\n            try {\n                if (in != null) {\n                    in.close();\n                }\n            } catch (IOException e) {\n                e.printStackTrace();\n            }\n            if (out != null) {\n                out.close();\n            }\n        }\n\n         return result;\n    }\n\n    private static class TrustAnyTrustManager implements X509TrustManager {\n\n        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {\n        }\n\n        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {\n        }\n\n        public X509Certificate[] getAcceptedIssuers() {\n            return new X509Certificate[] {};\n        }\n    }\n\n    private static class TrustAnyHostnameVerifier implements HostnameVerifier {\n        public boolean verify(String hostname, SSLSession session) {\n            return true;\n        }\n    }\n\n    public static void main(String[] args) {\n        HttpsProxy(&quot;https:\/\/www.baidu.com\/&quot;, &quot;&quot;, &quot;127.0.0.1&quot;, 81);\n        HttpProxy(&quot;http:\/\/www.appblog.cn\/&quot;, &quot;&quot;, &quot;127.0.0.1&quot;, 81);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u76f4\u63a5\u8bbe\u7f6e\u7cfb\u7edf\u5c5e\u6027\uff0c\u8bbe\u7f6e\u540e\u6240\u6709\u7f51\u7edc\u8bf7\u6c42\u90fd\u6709\u6548 System.setProperty(&quot;proxyTyp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-112","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=112"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/112\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}