public static String doHttpConnection(String urlStr, String postStr) {
String result = null;
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpPost = new HttpPost(urlStr);
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 120000);// 2分钟
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 420000);// 7分钟
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("info", postStr));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
String backResult = EntityUtils.toString(entity, HTTP.UTF_8);
JSONObject jsonObject = JSONObject.parseObject(backResult);
Object message = jsonObject.get("message");
if (message != null) {
result = message.toString();
}
} else {
result = "返回实体为空!";
}
} catch (SocketTimeoutException e) {
// TODO: handle exception
e.printStackTrace();
result = "读取超时!";
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
result = "接口调用异常!";
} finally {
httpclient.getConnectionManager().shutdown();
}
DefaultHttpClient访问url
猜你喜欢
转载自xianlincai.iteye.com/blog/2338792
今日推荐
周排行