您的位置:首页 > 理论基础 > 计算机网络

Java的HTTP通信

2016-06-16 12:52 381 查看
在Android中,HTTP通信可以用Volley,在Java中不能使用Volley,只能使用DefaultHttpClient,HttpPost和HttpResponse。

/*
* 向服务器发送数据,并接受返回的数据
*/
public static String send2Server(String url, List <NameValuePair> params){
String res = null;

// 建立HTTPPost连接
HttpPost httpRequest = new HttpPost(url);
try
{
// 发出HTTP Request
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 取得HTTP Response
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
res = EntityUtils.toString(httpResponse.getEntity());
}
catch (Exception e){
e.printStackTrace();
}

return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: