Httpclient发送json请求
一、Httpclient發(fā)送json請(qǐng)求
public String RequestJsonPost(String url){
??? String strresponse = null;
??? try{
??????? HttpClient hc = new DefaultHttpClient();
?????? HttpPost hp = new HttpPost(url);
?????? JSONObject jsonParam = new JSONObject();
?????? jsonParam.put("user","admin");
?????? jsonParam.put("password", "123456");
?????? //設(shè)置數(shù)據(jù)為utf-8編碼
?????? StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");
?????? //設(shè)置請(qǐng)求編碼
?????? entity.setContentEncoding("utf-8");
?????? //設(shè)置請(qǐng)求類型
?????? entity.setContentType("application/json");
?????? hp.setEntity(entity);
?????? //請(qǐng)求并得到結(jié)果
?????? HttpResponse result = hc.execute(hp);
?????? strresponse = EntityUtils.toString(result.getEntity(),"utf-8").trim();
??? }catch(Exception e){
?????? e.printStackTrace();
}
return strresponse;
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/beitha/p/6747853.html
總結(jié)
以上是生活随笔為你收集整理的Httpclient发送json请求的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。