少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

          package com.abin.lee.hack;

          import java.io.BufferedInputStream;

          import org.apache.http.HttpHost;
          import org.apache.http.HttpResponse;
          import org.apache.http.client.HttpClient;
          import org.apache.http.client.methods.HttpPost;
          import org.apache.http.entity.StringEntity;
          import org.apache.http.impl.client.DefaultHttpClient;
          import org.junit.Test;

          public class HttpClientVisitTest {

           private static final String HttpUrl = " private static final String HttpHost = "111.111.111.111";
           @Test
           public void testHttpClientVisit() {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(HttpUrl);
            httpPost.addHeader("Accept", "*/*");
            httpPost.addHeader("Accept-Language", "zh-cn");
            httpPost.addHeader("Referer", HttpUrl);
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
            httpPost.addHeader("Cache-Control", "no-cache");
            httpPost.addHeader("Accept-Encoding", "gzip, deflate");
            httpPost.addHeader("User-Agent",
              "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
            httpPost.addHeader("Host", HttpHost);
            httpPost.addHeader("Connection", "Keep-Alive");
          //  HttpHost httpProxy = new HttpHost("222.222.222.222", 1443, "http");
          //  httpClient.getParams().setParameter(AllClientPNames.DEFAULT_PROXY,httpProxy);
            StringBuffer params = new StringBuffer();
            String userName = "abin";
            String passWord= "varyall";
            String userAge= "12345";
            String homeTown= "china beijing";
            params.append("__userName ").append("=").append(userName )
              .append("&").append(passWord").append("=")
              .append(passWord).append("&").append("userAge")
              .append("=").append(userAge).append("&")
              .append("homeTown").append("=")
              .append(homeTown);
            HttpResponse httpResponse = null;
            String result = "";
            try {
             StringEntity reqEntity = new StringEntity(params.toString());
             httpPost.setEntity(reqEntity);
             HttpHost httpTarget = new HttpHost(HttpHost, 80, "http");
             httpResponse = httpClient.execute(httpTarget, httpPost);
             System.out.println("httpResponse=" + httpResponse.getStatusLine());

             BufferedInputStream buffer = new BufferedInputStream(httpResponse
               .getEntity().getContent());
             byte[] bytes = new byte[1024];
             int line = 0;
             StringBuilder builder = new StringBuilder();
             while ((line = buffer.read(bytes)) != -1) {
              builder.append(new String(bytes, 0, line));
             }
             result = new String(builder.toString());
            } catch (Exception e) {
             e.printStackTrace();
            } finally {
             if (httpPost.isAborted()) {
              httpPost.abort();
             }
             httpClient.getConnectionManager().shutdown();
            }
          //   System.out.println("result="+result);

           }

          }

          主站蜘蛛池模板: 巴林右旗| 河北区| 庆云县| 大荔县| 石景山区| 聂荣县| 娄底市| 冀州市| 木兰县| 炉霍县| 海晏县| 泊头市| 伊金霍洛旗| 镇康县| 广河县| 萨迦县| 彭山县| 涟水县| 红桥区| 上思县| 万年县| 房山区| 息烽县| 鸡西市| 永宁县| 揭东县| 宿松县| 波密县| 华安县| 壤塘县| 浑源县| 梅州市| 和静县| 木兰县| 临桂县| 石渠县| 平安县| 河北省| 前郭尔| 云霄县| 肥城市|