httpclient post请求例子(无参数名与带参数名的例子),多线程并发处理
版本:4.1
- 带参数名的情况
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
// httpPost.setHeader("Accept-Encoding", "gzip,deflate");//表示返回的数据是压缩的zip格式
String postParam = "";//请求的参数内容
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("data", postParam));
httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode() == 200) {if (entity.getContentEncoding().toString().equalsIgnoreCase("Content-Encoding: gzip")) {
response.setEntity(new GzipDecompressingEntity(response.getEntity())); //对zip进行解压
entity = response.getEntity();
}
String responseContent = EntityUtils.toString(entity);
System.out.println("responseContent: \n" + responseContent);
}
}
- 无参数名的情况
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
// httpPost.setHeader("Accept-Encoding", "gzip,deflate");//表示返回的数据是压缩的zip格式
String postParam = "";//请求的参数内容
StringEntity paramEntity = new StringEntity(postParam);//无参数名,只是参数内容
httpPost.setEntity(paramEntity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode() == 200) {if (entity.getContentEncoding().toString().equalsIgnoreCase("Content-Encoding: gzip")) {
response.setEntity(new GzipDecompressingEntity(response.getEntity())); //对zip进行解压
entity = response.getEntity();
}
String responseContent = EntityUtils.toString(entity);
System.out.println("responseContent: \n" + responseContent);
}
}
- httpEntity的类结构图

Httpclient并发处理处理:主要改变Httpclient对象的生成
/**
* 适合多线程的HttpClient,用httpClient4.2.1实现
* @return DefaultHttpClient
*/
public static DefaultHttpClient getHttpClient()
{
// 设置组件参数, HTTP协议的版本,1.1/1.0/0.9
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
HttpProtocolParams.setUseExpectContinue(params, true); //设置连接超时时间
int REQUEST_TIMEOUT = 60*1000; //设置请求超时60秒钟
int SO_TIMEOUT = 60*1000; //设置等待数据超时时间60秒钟
//HttpConnectionParams.setConnectionTimeout(params, REQUEST_TIMEOUT);
//HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, REQUEST_TIMEOUT);
params.setParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); //设置访问协议
SchemeRegistry schreg = new SchemeRegistry();
schreg.register(new Scheme("http",80,PlainSocketFactory.getSocketFactory()));
schreg.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); //多连接的线程安全的管理器
PoolingClientConnectionManager pccm = new PoolingClientConnectionManager(schreg);
pccm.setDefaultMaxPerRoute(20); //每个主机的最大并行链接数
pccm.setMaxTotal(100); //客户端总并行链接最大数 DefaultHttpClient httpClient = new DefaultHttpClient(pccm, params);
return httpClient;
}
httpclient post请求例子(无参数名与带参数名的例子),多线程并发处理的更多相关文章
- Spring MVC中forward请求转发2种方式(带参数)
Spring MVC中forward请求转发2种方式(带参数) http://www.51gjie.com/javaweb/956.html
- Qt 信号槽如何传递参数(或带参数的信号槽)
信号槽如何传递参数(或带参数的信号槽) 利用Qt进行程序开发时,有时需要信号槽来完成参数传递.带参数的信号槽在使用时,有几点需要注意的地 ...
- C#发送Post请求,带参数,不带参数,指定参数
1.不带参数发送Post请求 /// <summary> /// 指定Post地址使用Get 方式获取全部字符串 /// </summary> /// <param na ...
- c24--预编译,宏定义(带参数,不带参数)
// // main.c // 宏定义 #include <stdio.h> void test(); /* 预处理指令:在我们的文件翻译成0和1之前做的操作我们称之为预处理指令,一般以# ...
- 记录下httpclient 发送请求 服务端用@RequestBody 自动接收参数 报415
注解是post方式,那么检查以下内容:1. 你是否用了post请求2. 请求是否发送了数据3. 请求内容格式需要是 application/json .jquery 设置 contentType,-- ...
- Python装饰器实现带参数和不带参数
1 def log(text=None): 2 3 if isinstance(text, str): 4 def decorator(func): 5 @functools.wraps(func) ...
- Swift 函数做参数和闭包做参数的一个细节差别
函数作参数,示例为传入一个String和一个添加前缀的函数,返回一个添加完前缀的String: func demo(str:String,addPrefix:(String)->String)- ...
- JavaScript学习笔记 -- 带参数arguments的函数的用法
JavaScript函数有带参数与不带参数两种形式,不带参数情况如下: function myFunction() { alert('HelloWorld!') } 在这种类型的函数中,输出值是确定的 ...
- Qt带参数的信号和槽
在Qt的开发过程中,信号带参数是很常见的,在使用带参数的信号槽时,有以下几点需要注意. 当信号和槽函数的参数数量相同时,它们的参数类型要完全一致. 信号和槽函数的声明: signals: void i ...
随机推荐
- 使用libcurl显示下载进度
使用libcurl显示下载进度 http://blog.csdn.net/zhouzhenhe2008/article/details/53876622
- servlet访问路径的写法
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...
- 中国大陆被SCI收录的较高影响力期刊
1.清华主办的期刊Nano Research 清华新闻网2012年9月10日电:据汤森路透公司公布的2011年SCI期刊影响因子,清华大学主办的期刊Nano Research(纳米研究)的影响因子为6 ...
- 59. Spiral Matrix II (Array)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- WdatePicker.js的使用方法 帮助文档 使用说明(时间控件)
WdatePicker.js的使用方法 帮助文档 使用说明(时间控件) 4. 日期范围限制 静态限制 注意:日期格式必须与 realDateFmt 和 realTimeFmt 一致 你可以给通过配 ...
- Connection Lifetime
Connection Lifetime作用(数据库群集) 2010-11-12 14:17:01| 分类: sql server 2005 | 标签: |字号大http://xiangzhang ...
- java 主类的main方法调用其他方法
方法1:A a=new test().new A(); 内部类对象通过外部类的实例对象调用其内部类构造方法产生,如下: public class test{ class A{ void fA(){ S ...
- qt学习(二) buttong layout spinbox slider 示例
开启qt5 creator 新建项目 qt widgets 改写main.cpp #include "mainwindow.h" #include <QApplication ...
- 11 Mortal Fibonacci Rabbits
Problem Figure 4. A figure illustrating the propagation of Fibonacci's rabbits if they die after thr ...
- 使用delphi 开发多层应用(二十一)使用XE5 RESTClient 直接访问kbmmw 数据库
delphi XE5 出来了,增加了android 的开发支持,另外增加了一个RESTClient 来支持访问REST 服务器. 这个功能非常强大,可以直接使用非常多的REST 服务器.同时也可以支持 ...