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

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

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

找到办法过去。

 import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; public class HttpProcess{ private static HttpClient httpclient = new DefaultHttpClient();
public static void main(String[] args) { List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uin", "1633931117@qq.com"));
params.add(new BasicNameValuePair("appid", "522005705"));
params.add(new BasicNameValuePair("ptlang", "2052"));
params.add(new BasicNameValuePair("js_type", "2"));
params.add(new BasicNameValuePair("js_ver", "10009"));
String r = String.valueOf(Math.random());
params.add(new BasicNameValuePair("r", r));
String url = "https://ssl.ptlogin2.qq.com/check"; String body = post(url, params);
System.out.println(body);
} /**
* Get请求
*/
@SuppressWarnings("deprecation")
public static String get(String url, List<NameValuePair> params) {
String body = null;
try {
// Get请求
HttpGet httpget = new HttpGet(url);
// 设置参数
String str = EntityUtils.toString(new UrlEncodedFormEntity(params));
httpget.setURI(new URI(httpget.getURI().toString() + "?" + str));
// 发送请求
HttpResponse httpresponse = httpclient.execute(httpget);
// 获取返回数据
HttpEntity entity = httpresponse.getEntity();
body = EntityUtils.toString(entity);
if (entity != null) {
entity.consumeContent();
}
} catch (ParseException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return body;
} /**
* Post请求
*/
@SuppressWarnings("deprecation")
public static String post(String url, List<NameValuePair> params) {
String body = null;
try {
// Post请求
HttpPost httppost = new HttpPost(url);
// 设置参数
httppost.setEntity(new UrlEncodedFormEntity(params));
// 发送请求
HttpResponse httpresponse = httpclient.execute(httppost);
// 获取返回数据
HttpEntity entity = httpresponse.getEntity();
body = EntityUtils.toString(entity);
if (entity != null) {
entity.consumeContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return body;
} }

简单的HttpClient使用的更多相关文章

  1. 让服务调用更简单 - Caller.HttpClient

    前言 绝大多数项目都离不开服务调用,服务的调用方式通常是基于Http.RPC协议的调用,需要获取到对应服务的域名或者ip地址以及详细的控制器方法后才能进行调用,如果项目需要支持分布式部署,则需要借助服 ...

  2. HttpClient支持使用代理服务器以及身份认证

    HttpClient Authentication Doument: http://hc.apache.org/httpclient-3.x/authentication.html HttpClien ...

  3. HttpClient使用具体解释

    Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,添加�了易用性和灵活性(详细差别,日后我们再讨论),它不仅是client发送Http请求变得e ...

  4. ASP.NET Core 2.1 : 十三.httpClient.GetAsync 报SSL错误的问题

    不知什么时候 ,出现了这样的一个奇怪问题,简单的httpClient.GetAsync("xxxx")居然报错了.(ASP.NET Core 系列目录) 一.问题描述 把原来的程序 ...

  5. httpClient 深入浅出~

    本文偏重使用,简单讲述httpclient,其实在网络编程中,基于java的实现几乎都是包装了socket的通信,然后来模拟各种各样的协议:httpclient其实就是模拟浏览器发起想服务器端的请求, ...

  6. HttpClient4.5简单使用

    一.HttpClient简介 HttpClient是一个客户端的HTTP通信实现库,它不是一个浏览器.关于HTTP协议,可以搜索相关的资料.它设计的目的是发送与接收HTTP报文.它不会执行嵌入在页面中 ...

  7. HttpClient 教程 (三)

    转自:http://www.cnblogs.com/loveyakamoz/archive/2011/07/21/2113246.html 第三章 HTTP状态管理 原始的HTTP是被设计为无状态的, ...

  8. 网络相关系列之中的一个:Android中使用HttpClient发送HTTP请求

    一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 "超文本传输协议",是一种为分布式,合作式,多媒体信息系统服务,面向应用层的协议,是 ...

  9. java 接口自动化测试之数据请求的简单封装

    我们自己用java写接口自动化测试框架或者做个接口自动化测试平台的话,是需要自己进行相关的请求的,因此我们需要简单的封装下httpclient,我新建了一个http工具类,将get方法和post方法进 ...

随机推荐

  1. CPP - sort

    #include "stdafx.h" #include <iostream> #include <string> using namespace std; ...

  2. 常见的java类

    String System StringBuilder Thread Math ArrayList LinkedList HashMap HashSet Scanner Calendar Date F ...

  3. 《Linux内核设计与实现》CHAPTER17阅读梳理

    <Linux内核设计与实现>CHAPTER17阅读梳理 [学习时间:3.5hours] [学习内容:设备类型,模块,内核对象,sysfs] 个人思考部分见[]标出的部分 一.课堂讲解整理& ...

  4. js弹出框,禁刷新

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. sprintf数据库查询的作用

    $sql = sprintf("UPDATE file SET mimetype=null,title=null,size=null,protected=null WHERE id=%d&q ...

  6. golang 环境一键式配置

    在window下,通过以下bat,自动设置环境变量,启动终端,并cd到gopath目录 set goroot=c:\go set gopath=d:\go @start "start gol ...

  7. HTML5web存储之localStorage

    localStorage与cookie的作用类似,只能存储字符串,以键值对的方式进行存储:与cookie不同的是,可以存储更多的数据. localStorage用于持久化的本地存储. var skey ...

  8. hdfs shell 命令以及原理

    shell 操作 dfs 上传[hadoop@namenode ~]$ /data/hadoop/bin/hadoop fs -put /opt/MegaRAID/MegaCli/MegaCli64 ...

  9. swift language

    API reference Swift UIKit Swift 菜鸟教程 Great Installed Visual Studio Code, I found I cannot open it fr ...

  10. List转换成XML

    protected void Button1_Click(object sender, EventArgs e) { var customerlist = CustomerHelper.GetList ...