最近用到了,记录一下
导入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"为参数值