<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    有機肥

    綠色

    HttpClient4.X的代理添加實現(轉自http://blog.csdn.net/hblfyla/article/details/54962898)

    package org.yla.test;

    import java.net.URI;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpHost;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.Credentials;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.CredentialsProvider;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.BasicCredentialsProvider;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    import org.junit.Test;

    public class HttpClientTest {
        String url = "xxxxxxxxxxxxxxxxxxxxxxxxx";
        String ip = "202.107.233.85";
        int port = 8080;
        String username = "";
        String password = "";

        /**
         * 使用HttpClient4實現代理 202.107.233.85 8080
         *
         * @throws Exception
         */
        @Test
        public void test1() throws Exception {
            HttpClientBuilder build = HttpClients.custom();
            HttpHost proxy = new HttpHost(ip, port);
            CloseableHttpClient client = build.setProxy(proxy).build();
            HttpGet request = new HttpGet(url);
            CloseableHttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            System.out.println(EntityUtils.toString(entity));
        }

        /**
         * 使用httpclient3實現代理
         *
         * @throws Exception
         */
        @Test
        public void test2() throws Exception {
            HttpClient httpClient = new HttpClient();
            httpClient.getHostConfiguration().setProxy(ip, port);
            GetMethod method = new GetMethod(url);
            httpClient.executeMethod(method);
            String result = new String(method.getResponseBody());
            System.out.println(result);
        }

        /**
         * 使用httpclient4實現代理(帶密碼的代理)
         *
         * @throws Exception
         */
        @Test
        public void test3() throws Exception {
            HttpClientBuilder build = HttpClients.custom();
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            AuthScope authscope = new AuthScope(ip, port);
            Credentials credentials = new UsernamePasswordCredentials(username,
                    password);
            credentialsProvider.setCredentials(authscope, credentials);
            CloseableHttpClient client = build.setDefaultCredentialsProvider(
                    credentialsProvider).build();
            HttpGet request = new HttpGet(url);
            CloseableHttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            System.out.println(EntityUtils.toString(entity));
        }

        /**
         * 使用httpclient3實現代理(帶密碼的代理)
         *
         * @throws Exception
         */
        @Test
        public void test4() throws Exception {
            HttpClient httpClient = new HttpClient();
            org.apache.commons.httpclient.auth.AuthScope authscope = new org.apache.commons.httpclient.auth.AuthScope(
                    ip, port);
            org.apache.commons.httpclient.Credentials credentials = new org.apache.commons.httpclient.UsernamePasswordCredentials(
                    username, password);
            httpClient.getState().setProxyCredentials(authscope, credentials);
            GetMethod method = new GetMethod(url);
            httpClient.executeMethod(method);
            String result = new String(method.getResponseBody());
            System.out.println(result);
        }

        /**
         * 模擬登錄官網(http://mis.pyc.com.cn??
         *
         * @throws Exception
         */
        @Test
        public void testLogin() throws Exception {
            HttpClientBuilder build = HttpClients.custom();
            CloseableHttpClient client = build.build();
            HttpPost post = new HttpPost("http://mis.pyc.com.cn/login.aspx");
            List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
            params.add(new BasicNameValuePair("__VIEWSTATE",
                    "/wEPDwUJNjUwNzE0MTM4ZGQzh+vF2xGjdG8Q15kIqgR0CpxhmPucdCqZOPcglRZr/w=="));
            params.add(new BasicNameValuePair(
                    "__EVENTVALIDATION",
                    "/wEWBQLYtKSdCALEhISFCwKd+7qdDgKC3IeGDAK7q7GGCOqhJpRD8S8yy3ZAlPTSsmPzRUoXMK0mQvGgzlk6hm+G"));
            params.add(new BasicNameValuePair("txtName", "xxxxx"));
            params.add(new BasicNameValuePair("txtPwd", "xxxxxx"));
            params.add(new BasicNameValuePair("btnLogin", "xxxx"));
            HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
            post.setEntity(entity);
            CloseableHttpResponse response = client.execute(post);
            int statusCode = response.getStatusLine().getStatusCode();
            System.err.println("狀態" + statusCode);
            if (statusCode == 302) {
                Header[] location = response.getHeaders("location");
                String rediretUrl = null;
                if (location.length == 1) {
                    rediretUrl = "http://mis.pyc.com.cn" + location[0].getValue();
                    System.err.println("跳轉地址: " + rediretUrl);
                }
                Header[] allHeaders = response.getAllHeaders();
                System.out.println("==================response===================");
                for (Header header : allHeaders) {
                    System.err.println(header.getName() + ": " + header.getValue());
                }
                Header cookieHeader = response.getFirstHeader("Set-Cookie");
                String cookie = cookieHeader.getValue();
                System.out.println("cookie: " + cookie);
                HttpGet httpGet = new HttpGet(rediretUrl);
                httpGet.addHeader("Accept",
                        "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
                // httpGet.addHeader("Accept-Encoding", "gzip, deflate, sdch");
                // httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
                httpGet.addHeader("Connection", "keep-alive");
                httpGet.addHeader("Cookie", cookie);
                httpGet.addHeader("Host", "mis.pyc.com.cn");
                httpGet.addHeader("Referer", "http://mis.pyc.com.cn/login.aspx");
                httpGet.addHeader(
                        "User-Agent",
                        "ozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36");
                response = client.execute(httpGet);
                HttpEntity entity2 = response.getEntity();
                System.out
                        .println("----------------------------------------------");
                System.out.println(EntityUtils.toString(entity2));
            }
        }
    }

    posted on 2017-07-10 17:27 有機肥 閱讀(164) 評論(0)  編輯  收藏

    <2017年7月>
    2526272829301
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲伊人精品综合在合线| 亚洲成a人片在线观看日本| 中文字幕日本人妻久久久免费| 男人的天堂亚洲一区二区三区 | 真实国产乱子伦精品免费| 亚洲色精品vr一区二区三区| 国产做国产爱免费视频| 亚洲VA中文字幕无码毛片| 亚洲免费福利视频| 野花高清在线观看免费完整版中文| 亚洲一级免费毛片| 国产免费久久精品丫丫| 国产一卡2卡3卡4卡无卡免费视频 国产一卡二卡3卡四卡免费 | 在线免费一区二区| 国产精品久久久久久亚洲小说 | 国产99久久久久久免费看| 日韩免费一区二区三区在线播放| 亚洲成AV人综合在线观看| 插鸡网站在线播放免费观看| 亚洲中文字幕久久精品无码喷水| 成人久久免费网站| 亚洲综合激情视频| 国内自产少妇自拍区免费| 免费无码婬片aaa直播表情| 亚洲午夜未满十八勿入网站2| 无码专区AAAAAA免费视频| 亚洲综合色区中文字幕| 吃奶摸下高潮60分钟免费视频| 中国精品一级毛片免费播放| 亚洲一区二区影院| 成人免费看吃奶视频网站| 在线综合亚洲欧洲综合网站| 国产无遮挡吃胸膜奶免费看视频 | 久久综合亚洲色HEZYO社区| 黄色网页在线免费观看| 久久久久久亚洲Av无码精品专口| 最近中文字幕mv免费高清电影| 一级毛片免费播放男男| 国产福利电影一区二区三区,亚洲国模精品一区| 亚洲人成电影院在线观看| 国产一级淫片视频免费看|