package util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.hzlq.gdlh.dto.ReqResult;
public class HttpClient {
private static Logger logger = LoggerFactory.getLogger(HttpClient.class);
private static final int timeoutConnection = 60000;
private static final int timeoutSocket = 60000;
public static HttpResponse get(String url) throws Exception {
url=url.replace(" ", "%20");
logger.debug("=====http get url[{}]=====", url);
HttpParams myParams = new BasicHttpParams();
// 2s连接不上就超时
HttpConnectionParams.setConnectionTimeout(myParams, 20 * 1000);
// 300s数据读取未完成,超时
HttpConnectionParams.setSoTimeout(myParams, 300 * 1000);
DefaultHttpClient client = new DefaultHttpClient(myParams);
HttpResponse response = null;
try {
HttpGet get = new HttpGet(url);
response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
logger.debug("====http get url[{}] success====", url);
} else {
logger.error("====返回状态错误:\n{}====", response.toString());
throw new Exception("http get请求失败");
}
} catch (UnsupportedEncodingException e) {
logger.error("请求外部服务[{}]失败", url);
logger.error("ERROR", e);
throw e;
} catch (ClientProtocolException e) {
logger.error("请求外部服务[{}]失败", url);
logger.error("ERROR", e);
throw e;
} catch (Exception e) {
logger.error("请求外部服务[{}]失败", url);
logger.error("ERROR", e);
throw e;
} finally {
client.getConnectionManager().shutdown();
return response;
}
}
/*
ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
pairs.add(new BasicNameValuePair(参数1,值1));
pairs.add(new BasicNameValuePair(参数2,值2));
pairs.add(new BasicNameValuePair(参数3,值3));
pairs.add(new BasicNameValuePair(参数4,值4));
*/
public static ReqResult post(String url , ArrayList<BasicNameValuePair> pairs) throws Exception {
HttpPost httpPost = new HttpPost(url);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpResponse httpResponse;
try {
httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8")); //
httpResponse = httpclient.execute(httpPost);
int status = httpResponse.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_OK) {
logger.error("调用企业的统一http接口出错,返回的status:" + status);
} else {
InputStream inStream = null;
BufferedReader reader = null;
try {
inStream = httpResponse.getEntity().getContent();
reader = new BufferedReader(new InputStreamReader(inStream,
"utf-8"));
StringBuilder strber = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
strber.append(line);
}
logger.info("调用企业的统一http接口 返回信息 result:" + strber);
ReqResult res = JSON.parseObject(strber.toString(),
ReqResult.class);
return res;
} catch (Exception e) {
logger.error("", e);
} finally {
if (inStream != null) {
inStream.close();
}
if (reader != null) {
reader.close();
}
}
}
} catch (UnsupportedEncodingException e1) {
logger.error("", e1);
} catch (ClientProtocolException e1) {
logger.error("", e1);
} catch (IOException e1) {
logger.error("", e1);
} finally {
if (httpclient != null) {
httpclient.getConnectionManager().shutdown();
}
}
return null;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.hzlq.gdlh.dto.ReqResult;
public class HttpClient {
private static Logger logger = LoggerFactory.getLogger(HttpClient.class);
private static final int timeoutConnection = 60000;
private static final int timeoutSocket = 60000;
public static HttpResponse get(String url) throws Exception {
url=url.replace(" ", "%20");
logger.debug("=====http get url[{}]=====", url);
HttpParams myParams = new BasicHttpParams();
// 2s连接不上就超时
HttpConnectionParams.setConnectionTimeout(myParams, 20 * 1000);
// 300s数据读取未完成,超时
HttpConnectionParams.setSoTimeout(myParams, 300 * 1000);
DefaultHttpClient client = new DefaultHttpClient(myParams);
HttpResponse response = null;
try {
HttpGet get = new HttpGet(url);
response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
logger.debug("====http get url[{}] success====", url);
} else {
logger.error("====返回状态错误:\n{}====", response.toString());
throw new Exception("http get请求失败");
}
} catch (UnsupportedEncodingException e) {
logger.error("请求外部服务[{}]失败", url);
logger.error("ERROR", e);
throw e;
} catch (ClientProtocolException e) {
logger.error("请求外部服务[{}]失败", url);
logger.error("ERROR", e);
throw e;
} catch (Exception e) {
logger.error("请求外部服务[{}]失败", url);
logger.error("ERROR", e);
throw e;
} finally {
client.getConnectionManager().shutdown();
return response;
}
}
/*
ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
pairs.add(new BasicNameValuePair(参数1,值1));
pairs.add(new BasicNameValuePair(参数2,值2));
pairs.add(new BasicNameValuePair(参数3,值3));
pairs.add(new BasicNameValuePair(参数4,值4));
*/
public static ReqResult post(String url , ArrayList<BasicNameValuePair> pairs) throws Exception {
HttpPost httpPost = new HttpPost(url);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpResponse httpResponse;
try {
httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8")); //
httpResponse = httpclient.execute(httpPost);
int status = httpResponse.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_OK) {
logger.error("调用企业的统一http接口出错,返回的status:" + status);
} else {
InputStream inStream = null;
BufferedReader reader = null;
try {
inStream = httpResponse.getEntity().getContent();
reader = new BufferedReader(new InputStreamReader(inStream,
"utf-8"));
StringBuilder strber = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
strber.append(line);
}
logger.info("调用企业的统一http接口 返回信息 result:" + strber);
ReqResult res = JSON.parseObject(strber.toString(),
ReqResult.class);
return res;
} catch (Exception e) {
logger.error("", e);
} finally {
if (inStream != null) {
inStream.close();
}
if (reader != null) {
reader.close();
}
}
}
} catch (UnsupportedEncodingException e1) {
logger.error("", e1);
} catch (ClientProtocolException e1) {
logger.error("", e1);
} catch (IOException e1) {
logger.error("", e1);
} finally {
if (httpclient != null) {
httpclient.getConnectionManager().shutdown();
}
}
return null;
}
}