首页 > 技术文章 > HttpClient,DefaultHttpClient使用

zhengzhongqi 2019-08-19 11:06 原文

public static String XXXPost(String url, String json)throws Exception{

//创建一个DefaultHttpClient的实例

HttpClient httpClient = new DefaultHttpClient();

//创建一个HttpPost对象,传入目标的网络地址:

HttpPost post = new HttpPost(url);

//参数传递 

StringEntity postingString = new StringEntity(json);// json传递
postingString.setContentType("application/x-www-form-urlencoded");

//参数传入 

post.setEntity(postingString);

//执行execute()方法之后会返回一个HttpResponse对象,服务器所返回的所有信息就保护在HttpResponse里面.

先取出服务器返回的状态码,如果等于200就说明请求和响应都成功了:

HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
return content;
}

下面是一位大神的详细用法:

https://www.cnblogs.com/xiayahui/p/5623947.html 

 

推荐阅读