简单的HttpClient使用
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使用的更多相关文章
- 让服务调用更简单 - Caller.HttpClient
前言 绝大多数项目都离不开服务调用,服务的调用方式通常是基于Http.RPC协议的调用,需要获取到对应服务的域名或者ip地址以及详细的控制器方法后才能进行调用,如果项目需要支持分布式部署,则需要借助服 ...
- HttpClient支持使用代理服务器以及身份认证
HttpClient Authentication Doument: http://hc.apache.org/httpclient-3.x/authentication.html HttpClien ...
- HttpClient使用具体解释
Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,添加�了易用性和灵活性(详细差别,日后我们再讨论),它不仅是client发送Http请求变得e ...
- ASP.NET Core 2.1 : 十三.httpClient.GetAsync 报SSL错误的问题
不知什么时候 ,出现了这样的一个奇怪问题,简单的httpClient.GetAsync("xxxx")居然报错了.(ASP.NET Core 系列目录) 一.问题描述 把原来的程序 ...
- httpClient 深入浅出~
本文偏重使用,简单讲述httpclient,其实在网络编程中,基于java的实现几乎都是包装了socket的通信,然后来模拟各种各样的协议:httpclient其实就是模拟浏览器发起想服务器端的请求, ...
- HttpClient4.5简单使用
一.HttpClient简介 HttpClient是一个客户端的HTTP通信实现库,它不是一个浏览器.关于HTTP协议,可以搜索相关的资料.它设计的目的是发送与接收HTTP报文.它不会执行嵌入在页面中 ...
- HttpClient 教程 (三)
转自:http://www.cnblogs.com/loveyakamoz/archive/2011/07/21/2113246.html 第三章 HTTP状态管理 原始的HTTP是被设计为无状态的, ...
- 网络相关系列之中的一个:Android中使用HttpClient发送HTTP请求
一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 "超文本传输协议",是一种为分布式,合作式,多媒体信息系统服务,面向应用层的协议,是 ...
- java 接口自动化测试之数据请求的简单封装
我们自己用java写接口自动化测试框架或者做个接口自动化测试平台的话,是需要自己进行相关的请求的,因此我们需要简单的封装下httpclient,我新建了一个http工具类,将get方法和post方法进 ...
随机推荐
- 骑士游历/knight tour - visual basic 解决
在visual baisc 6 how to program 中文版第七章的练习题上看到了这个问题,骑士游历的问题. 在8x8的国际象棋的棋盘上,骑士(走法:一个方向走两格,另一个方向一格)不重复走完 ...
- swift-UITableView
import UIKit class FirstVC: UIViewController,UITableViewDelegate,UITableViewDataSource { var tableVi ...
- Unity透明材质Batch
NO Batch ? 游戏场景中存在大量例子的时候,DrallCall的压力很大,但是遍历一遍之后发现,为啥一样的粒子特效竟然没有合并,why?经过很多测试后发现,如果把透明材质的修改为非半透明的, ...
- 时间js转换方法Date("149...") 转成 2016-7-12 21:23:34 009
function timeFormatter(value) { var da = new Date(parseInt(value.replace("/Date(", &q ...
- Ant和Maven的作用是什么?两者之间功能、特点有哪些区别?
Ant和Maven都是基于Java的构建(build)工具.理论上来说,有些类似于(Unix)C中的make ,但没有make的缺陷. Ant是软件构建工具,Maven的定位是软件项目管理和理解工具. ...
- LeakCanary 内存检测 工具 --超级傻瓜 不会DDMS的福音
大神资料贴出 ,学习 ,集成到项目中 . LeakCanary 中文使用说明 http://www.liaohuqiu.net/cn/posts/leak-canary/ logcat 显示 ...
- 【原创】Android selector选择器无效或无法正常显示的一点研究
想将LinearLayout作为一个按钮,加上一个动态背景,按下的时候,背景变色,这个理所当然应该使用selector背景选择器来做: <LinearLayout android:id=&quo ...
- linux udev 自动挂载 SD卡/U盘
本文记录使用udev自动挂载SD卡和U盘的方法. 参考链接 http://blog.chinaunix.net/uid-26119896-id-5211736.html 添加udev规则 创建文件/e ...
- Java c3p0连接池之二
<?xml version="1.0" encoding="UTF-8"?> <!-- c3p0-config.xml文件配置 --> ...
- delphi之TDataset
最近遇到了很多问题,现在做一下总结. 字符串处理: 字符串相加 var S1, S2: String; begin S1 := Concat('A', 'B'); // 连接两个字符串,S1变量等于A ...