HttpClient,post请求,发送json,并接收数据
String URL ="www.baidu.com";
HttpClient http = new HttpClient(); //创建http连接, 相当于,打开浏览器
PostMethod post = new PostMethod(URL); //创建制定URL地址的PostMethod对象并值定, 相当于在浏览器上输入网址
String result = "";
try {
RequestEntity re = new StringRequestEntity(json.toString(), "application/json", "UTF-8"); //设置请求体(数据,格式,编码)
post.setRequestEntity(re);
post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); //使用默认的恢复策略
post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 10*1000); //设置超时时间
if(http.executeMethod(post)==200){ //响应正常
String result = post.getResponseBodyAsString() //获取数据
result = new String(result.getBytes("ISO-8859-1"), "UTF-8");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(post!=null){
post.releaseConnection();
}
}