public String doPost(String url, Map<String, String> params) {
String response = null;
log.info("url为:" + url + ",params为:" + params);
HttpClient httpClient = new HttpClient();// 创建一个客户端,类似打开一个浏览器
httpClient.getParams().setContentCharset("utf-8");// 设置编码格式
PostMethod postMethod = new PostMethod(url);// 创建一个post方法,类似在浏览器地址栏中输入一个地址
NameValuePair[] postData = new NameValuePair[params.size()];// POST需要通过NameValuePair类来设置参数名称和它所对应的值
// 设置Http Post数据
if (params != null) {
int count = 0;
for (Map.Entry<String, String> entry : params.entrySet()) {
postData[count] = new NameValuePair(entry.getKey(), entry.getValue());
count++;
}
postMethod.addParameters(postData);
}
try {
log.info("执行post方法开始");
httpClient.executeMethod(postMethod);// 执行,类似于回车键
log.info("执行post方法结束");
log.info("method.getStatusCode()=" + postMethod.getStatusCode());
if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
log.info("获取返回值开始");
response = postMethod.getResponseBodyAsString();
log.info("获取返回值结束,response为:" + response);
}
} catch (Exception e) {
log.error("post发生异常:" + e);
e.printStackTrace();
} finally {
postMethod.releaseConnection();// 释放
log.info("释放链接");
}
return response;
}
对于编码方式可以有以下几种方式,应该都可以的,我只用了其中一种:httpClient.getParams().setContentCharset("utf-8");// TODO
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");// TODO
postMethod.getParams().setContentCharset("utf-8");// TODO
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");// TODO
postMethod.setQueryString(EncodingUtil.formUrlEncode(postData, "utf-8"));// TODO
分享到:
相关推荐
import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; ...
org.apache.http jar包 import org.apache.http.Header;...import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; ...
import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; ...
import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; ...
import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; ...
org.apache.http post/get 解决中文乱码 相关的jar~~~
httpmime-4.5.2.jar httpcore-4.4.4.jar httpclient-win-4.5.2.jar httpclient-cache-4.5.2.jar httpclient-4.5.2.jar 提供以上5个jar包
参照网上资料,写的一个使用apachehttp模块,进行资源下载的demo.
安装配置apache并修改对应配置实现访问地址的重定向功能,已apache所在服务器为中间服务器来完成访问地址的跳转
下面用Jmeter发送一个post请求, 对应的js代码如下: $(#register_a).click(function() { var name = $(#uname).val(); var pwd = $(#pwd).val(); var email = $(#email).val(); ...
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; ...
很多apache日志分析工具都是要...6、请求方式(POST、GET等)统计分析; 7、请求协议统计分析。 为了更加灵活且简化工具大小,工具会将统计分析的结果导出到EXCEL或WPS里面,用户可以利用EXCEL或WPS灵活生成图形报表。
通过Apache的httpClient的get, post 请求下载,或者上传文件
apache benchmark 独立文件 ab.exe 可以直接使用 Version 2.3。一般用户压力测试用。参数如下 .\ab.exe --help Options are: -n requests Number of requests to perform -c concurrency Number of multiple ...
5.1 it is also required to apply at least the NWLIB6e Post Service Pack update. ATTENTION: this archive extracts to \APACHE24 !! The httpd 2.4 module API is not compatible with previous versions!
SOAP通常在HTTP上交换信息:客户端POST一个SOAP请求,然后接受HTTP响应,HTTP的响应可能为成功代码、SOAP响应或者错误代码。有需要的朋友可以下载看看。资源截图: 资源太大,传百度网盘了,链接在附件中,有需要的...
用apache必须要添加的包,httpClient的post方法和post方法