HttpClient的网络请求:
直接上代码:
<pre name="code" class="java">package com.matou.tradeerp.util;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
public class NetworkRequest {
//网络解析公共类
public static String getResult(String uri){
String result = "-3";
//返回的response
HttpResponse response = null;
// 定义HttpClient
HttpClient client = null;
try {
client = new DefaultHttpClient();
HttpGet get = new HttpGet(uri);
response = client.execute(get);
<span style="white-space:pre"> </span>//获得网络请求的实体
HttpEntity entity = response.getEntity();
//获取状态码
HttpParams httpParams = new BasicHttpParams();
//设置连接超时的事件
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
//返回的code码
// response.getStatusLine().getStatusCode();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(client!=null){
//释放Client资源
client = null;
}
if(response!=null){
response = null;
}
}
return result;
}
}