Java HTTP请求

时间:2022-06-02 21:45:32

注意:java  http请求要放在 try catch里面,该过程是一个阻塞过程,所以需要新建一个线程进行处理

        try
{
HttpPost request = new HttpPost(URL);
StringEntity se = new StringEntity(param.toString(), HTTP.UTF_8);
se.setContentType("application/json");
request.setEntity(se);
HttpResponse httpResponse = new DefaultHttpClient().execute(request);
// 得到应答的字符串,这也是一个 JSON 格式保存的数据
String retSrc = EntityUtils.toString(httpResponse.getEntity());
retSrc = URLDecoder.decode(retSrc, "UTF-8");
}
catch (Exception e)
{
e.printStackTrace();
}