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

一个http请求二次请求才能获取到数据,为什么,url的问题?

2016-12-05 13:36 676 查看
public static void t(String[] args) throws ClientProtocolException, IOException, InterruptedException{
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("http://172.19.10.137:8601/bjt1deea3e3e7fb/js/data/user_info.json");
List<NameValuePair>params=new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("codeType","bno"));

params.add(new BasicNameValuePair("userNumber","B23871039"));
params.add(new BasicNameValuePair("business","data"));

UrlEncodedFormEntity entity=new UrlEncodedFormEntity(params,"utf-8");
httpPost.setEntity(entity);
HttpResponse httpResponse1 =httpClient.execute(httpPost);
//释放  不知道为什么不释放一次取不到返回
httpPost.releaseConnection();
HttpResponse httpResponse =httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200){

//请求和响应都成功了

HttpEntity rentity=httpResponse.getEntity();//调用getEntity()方法获取到一个HttpEntity实例

String response=EntityUtils.toString(rentity,"utf-8");//用EntityUtils.toString()这个静态方法将HttpEntity转换成字符串,防止服务器返回的数据带有中文,所以在转换的时候将字符集指定成utf-8就可以了
System.out.println("-------------------------------------");
System.out.println(response);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java http
相关文章推荐