HttpClient,DefaultHttpClient使用


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 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM