httpclient不带参数获取值
2016-06-20 00:00
369 查看
客户端
public String postFormCom(String url, String params) {
// 创建默认的httpClient实例.
HttpClient httpclient = HttpClientBuilder.create().build();
// 创建httppost
HttpPost httppost = new HttpPost(url);
try {
StringEntity reqEntity = new StringEntity(params, "utf-8");
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getURI());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("--------------------------------------");
String responseStr = EntityUtils.toString(entity, "utf-8");
System.out.println("Response content: " + responseStr);
System.out.println("--------------------------------------");
return responseStr;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httppost.releaseConnection();
}
return null;
}
服务器端
@RequestMapping( { "/test.htm" })
public void test(HttpServletRequest request, HttpServletResponse response) throws Exception {
InputStream is = request.getInputStream();
System.out.println(convertStreamToString(is));
}
public String convertStreamToString(InputStream is) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null){
buffer.append(line).append("\n");
}
return buffer.toString();
}
public String postFormCom(String url, String params) {
// 创建默认的httpClient实例.
HttpClient httpclient = HttpClientBuilder.create().build();
// 创建httppost
HttpPost httppost = new HttpPost(url);
try {
StringEntity reqEntity = new StringEntity(params, "utf-8");
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getURI());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("--------------------------------------");
String responseStr = EntityUtils.toString(entity, "utf-8");
System.out.println("Response content: " + responseStr);
System.out.println("--------------------------------------");
return responseStr;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httppost.releaseConnection();
}
return null;
}
服务器端
@RequestMapping( { "/test.htm" })
public void test(HttpServletRequest request, HttpServletResponse response) throws Exception {
InputStream is = request.getInputStream();
System.out.println(convertStreamToString(is));
}
public String convertStreamToString(InputStream is) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null){
buffer.append(line).append("\n");
}
return buffer.toString();
}
相关文章推荐
- iOS9下使用http请求
- 流亡“***”分子向中国发起网络战
- 放弃我最喜爱的网络,全面转入软件开发
- 网络连接状态检测服务 小工具
- 大型银行核心网络的三层结构设计
- EDIMAX中小企业网络解决方案
- [转]TCP的拥塞控制
- Android 一个改善的okHttp封装库
- HttpClient4.2 Fluent API学习
- linux网络编程----->项目管理工具----->Makefile
- Android Volley增加Https支持,允许SSL
- TCP通信的三次握手和四次撒手的详细流程
- HTTP缓存缓存机制
- TCP的SO_LINGER 选项
- 让chrome谷歌浏览器自动访问https链接
- 基于C#的简单http服务器,用于多台工控机数据的监控
- Linux网络配置 DNS配置信息丢失 可能原因分析
- HTTP错误405-Method Not Allowed
- 玩营销就要做潜伏,原来奇瑞是这么干的
- 开源一个网络库ARHttpRequest,iOS适配NSURLSession/ASIHTTPRequest等,Android适配HttpURLConnection等