java URL代理

来源:互联网 发布:sql为列设置默认值 编辑:程序博客网 时间:2024/04/28 04:34
private String makeHttpRequest(String requestUrl, HtmlMethod htmlMethod, HashMap<String, String> headers, String bodyText) throws Exception {
        String responseStr = "";
        HttpURLConnection urlConn = null;
        try {
//            AxisProperties.setProperty("http.proxyHost", "172.20.230.8");  
//            AxisProperties.setProperty("http.proxyPort", "808");
//            
//            AxisProperties.setProperty("https.proxyHost", "172.20.230.8");  
//            AxisProperties.setProperty("https.proxyPort", "808");
            
            String useProxy = "true";
            String host = "172.20.230.8";
            String port = "808";
            System.getProperties().put("http.proxySet", useProxy);
            System.getProperties().put("http.proxyHost", host);
            System.getProperties().put("http.proxyPort", port);
            System.getProperties().put("https.proxySet", useProxy);
            System.getProperties().put("https.proxyHost", host);
            System.getProperties().put("https.proxyPort", port);
            
            URL _url = new URL(requestUrl);
            URLConnection conn = _url.openConnection();

            if (requestUrl.toLowerCase().startsWith("https:")) {
                urlConn = (HttpsURLConnection) conn;
            } else {
                urlConn = (HttpURLConnection) conn;
            }

            if (headers != null && headers.keySet().size() > 0) {
                for (String key : headers.keySet()) {
                    String value = headers.get(key);
                    urlConn.setRequestProperty(key, value);
                }
            }

            if (htmlMethod == HtmlMethod.GET) {
                urlConn.setRequestMethod("GET");
            } else if (htmlMethod == HtmlMethod.POST) {
                urlConn.setDoOutput(true);
                urlConn.setRequestMethod("POST");
            } else if (htmlMethod == HtmlMethod.PUT) {
                urlConn.setDoOutput(true);
                urlConn.setRequestMethod("PUT");
            }
            //urlConn.setConnectTimeout(10000);
            //urlConn.setReadTimeout(10000);
            urlConn.connect();

            if ((htmlMethod == HtmlMethod.POST || htmlMethod == HtmlMethod.PUT) && bodyText != null && bodyText.length()>0) {
                OutputStream os = urlConn.getOutputStream();
                os.write(bodyText.getBytes("UTF-8"));
                os.flush();
            }

            InputStream dataInput = null;
            if (urlConn.getResponseCode() >= 400) {
                dataInput = urlConn.getErrorStream();
                responseStr = IOUtils.toString(dataInput, "UTF-8");
                throw new ApiException(responseStr);
            } else {
                dataInput = urlConn.getInputStream();
            }
            responseStr = IOUtils.toString(dataInput, "UTF-8");
        } finally {
            if (urlConn != null) {
                urlConn.disconnect();
            }
        }
        return responseStr;
    }
0 0
原创粉丝点击