使用HttpClient发送请求并接收返回信息

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/javadogisme/article/details/78893762

最近用到了,记录一下

导入HttpClient的jar包,我这里用的是4.5.3版本



关键代码如下

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://192.168.1.123:8080/ries_interface/loginServlet");			
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("msg","toBeSendMsg"));
UrlEncodedFormEntity uefEntity;
uefEntity = new UrlEncodedFormEntity(params,"UTF-8");
httpPost.setEntity(uefEntity);
log.info("执行request请求"+httpPost.getURI());
HttpResponse httpResponse;
httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
String resData = EntityUtils.toString(entity);			
log.info("返回信息:"+resData);


说明:

params.add(new BasicNameValuePair("msg", “toBeSendMsg”));
这里是存放了发送的信息,"msg"为参数名,"toBeSendMsg"为参数值

展开阅读全文

没有更多推荐了,返回首页