一個http請求二次請求才能獲取到數據,爲什麼,url的問題?

 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);
    		}
    	}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章