zoukankan      html  css  js  c++  java
  • 简单的HttpClient使用

    Httpclient用途很广泛,用来处理各种http请求,这里举个简单的例子

    去查询QQ邮件登陆账号检测是的verifycode,一直想怎么能够代码登陆

    QQ邮箱,但是QQ的登陆机制做的太TMD牛逼了,验证码一关还没

    找到办法过去。

      1 import java.io.IOException;
      2 import java.io.UnsupportedEncodingException;
      3 import java.net.URI;
      4 import java.net.URISyntaxException;
      5 import java.util.ArrayList;
      6 import java.util.List;
      7 
      8 import org.apache.http.HttpEntity;
      9 import org.apache.http.HttpResponse;
     10 import org.apache.http.NameValuePair;
     11 import org.apache.http.ParseException;
     12 import org.apache.http.client.ClientProtocolException;
     13 import org.apache.http.client.HttpClient;
     14 import org.apache.http.client.entity.UrlEncodedFormEntity;
     15 import org.apache.http.client.methods.HttpGet;
     16 import org.apache.http.client.methods.HttpPost;
     17 import org.apache.http.impl.client.DefaultHttpClient;
     18 import org.apache.http.message.BasicNameValuePair;
     19 import org.apache.http.util.EntityUtils;
     20 
     21 public class HttpProcess{
     22 
     23     private static HttpClient httpclient = new DefaultHttpClient();
     24     public static void main(String[] args) {
     25         
     26         List<NameValuePair> params = new ArrayList<NameValuePair>();
     27         params.add(new BasicNameValuePair("uin", "1633931117@qq.com"));
     28         params.add(new BasicNameValuePair("appid", "522005705"));
     29         params.add(new BasicNameValuePair("ptlang", "2052"));
     30         params.add(new BasicNameValuePair("js_type", "2"));
     31         params.add(new BasicNameValuePair("js_ver", "10009"));
     32         String r =  String.valueOf(Math.random());
     33         params.add(new BasicNameValuePair("r", r));
     34         String url = "https://ssl.ptlogin2.qq.com/check";
     35 
     36         String body = post(url, params);
     37         System.out.println(body);
     38     }
     39 
     40     /**
     41      * Get请求
     42      */
     43     @SuppressWarnings("deprecation")
     44     public static String get(String url, List<NameValuePair> params) {
     45         String body = null;
     46         try {
     47             // Get请求
     48             HttpGet httpget = new HttpGet(url);
     49             // 设置参数
     50             String str = EntityUtils.toString(new UrlEncodedFormEntity(params));
     51             httpget.setURI(new URI(httpget.getURI().toString() + "?" + str));
     52             // 发送请求
     53             HttpResponse httpresponse = httpclient.execute(httpget);
     54             // 获取返回数据
     55             HttpEntity entity = httpresponse.getEntity();
     56             body = EntityUtils.toString(entity);
     57             if (entity != null) {
     58                 entity.consumeContent();
     59             }
     60         } catch (ParseException e) {
     61             e.printStackTrace();
     62         } catch (UnsupportedEncodingException e) {
     63             e.printStackTrace();
     64         } catch (IOException e) {
     65             e.printStackTrace();
     66         } catch (URISyntaxException e) {
     67             e.printStackTrace();
     68         }
     69         return body;
     70     }
     71 
     72     /**
     73      *  Post请求
     74      */
     75     @SuppressWarnings("deprecation")
     76     public static String post(String url, List<NameValuePair> params) {
     77         String body = null;
     78         try {
     79             // Post请求
     80             HttpPost httppost = new HttpPost(url);
     81             // 设置参数
     82             httppost.setEntity(new UrlEncodedFormEntity(params));
     83             // 发送请求
     84             HttpResponse httpresponse = httpclient.execute(httppost);
     85             // 获取返回数据
     86             HttpEntity entity = httpresponse.getEntity();
     87             body = EntityUtils.toString(entity);
     88             if (entity != null) {
     89                 entity.consumeContent();
     90             }
     91         } catch (UnsupportedEncodingException e) {
     92             e.printStackTrace();
     93         } catch (ClientProtocolException e) {
     94             e.printStackTrace();
     95         } catch (ParseException e) {
     96             e.printStackTrace();
     97         } catch (IOException e) {
     98             e.printStackTrace();
     99         }
    100         return body;
    101     }
    102 
    103 }
    View Code
  • 相关阅读:
    记intouch SMC local下驱动丢失问题解决
    随机变量的频数分布图
    Clonal hematopoiesis of indeterminate potential(意义不明的克隆性造血)-CHIP
    聚类热图
    linux把软件安装到指定目录
    Phred-scale quality scores的相关内容
    GATK的硬过滤
    VCF和GVCF格式说明
    非root权限 安装更新gcc
    在非小细胞肺癌中,MET基因的14号外显子突变和年龄,依赖于癌症阶段的CNV,C-MET过表达的关系
  • 原文地址:https://www.cnblogs.com/cstar/p/3467318.html
Copyright © 2011-2022 走看看