httpClient
来源:互联网 发布:淘宝店铺二维码在哪里 编辑:程序博客网 时间:2024/06/05 14:45
当同时使用httpUrlConnection和httpClient访问数据时
在用httpclient是会出现
SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
get请求
HttpClient client = new DefaultHttpClient();String path = "网络路径";HttpGet httpGet = new HttpGet(path);HttpResponse httpResponse = client.execute(httpGet);int statusCode = httpResponse.getStatusLine().getStatusCode();if (statusCode == 200){ InputStream inputStream = httpResponse.getEntity().getContent();
}
post请求
//有一个请求的http客户端对象HttpClient client = new DefaultHttpClient();
String path = "http://v.juhe.cn/toutiao/index";
//指定请求方式的对象
HttpPost httpPost = new HttpPost(path);
//创建传递参数的集合....并且把传递的参数放到集合中
List<BasicNameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("type","top"));
params.add(new BasicNameValuePair("key","597b4f9dcb50e051fd725a9ec54d6653"));
//创建一个请求实体内容的对象,,,,UrlEncodedFormEntity支持url编码,并且支持form格式
//list<? extends NameValuePair> params 要给服务器传递的参数,,,所有的参数需要放到集合里面,string encoding指定编码字符集
HttpEntity entity = new UrlEncodedFormEntity(params,"utf-8");
//http协议中,post请求方式,请求的参数是在请求的实体内容中....setEntity设置请求实体内容的对象
httpPost.setEntity(entity);
//执行post请求
HttpResponse httpResponse = client.execute(httpPost);
//获取
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200){
//获取到响应的字节流
InputStream inputStream = httpResponse.getEntity().getContent();
}
- HttpClient
- HttpClient
- httpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- httpclient
- httpclient
- HttpClient
- HttpClient
- httpclient
- httpclient
- HttpClient
- More Effective C++学习笔记-条款5|6
- enumerate用法总结
- MongoDB学习笔记:(1)、MongoDB在Win10下的安装
- python Day8 :集合
- Dynamic Right-Sizing for Power-Proportional Data Centers文章阅读笔记
- httpClient
- pandas 基础
- 边缘检测方法
- Hadoop学习
- 初窥javaScrit权威指南,第六章对象(2)
- VS中如何设置自己的快捷代码段
- 深拷贝deepcopy与浅拷贝copy
- Docker 最常用的监控方案
- html