<pre name="code" class="java"><pre name="code" class="java">package com.ulex.ulextest.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
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.protocol.HTTP;
import org.apache.http.Header;
public class HttpUtil {
private static final int HTTP_CONNECTION_TIMEOUT = 10000;// 连接超时
private static final int HTTP_SO_TIMEOUT = 10000;// 请求超时
/**
* HttpClient-post
*/
public static String httpPost(String url, Map<String, String> map) {
String response = null;
List<NameValuePair> params = null;
if (map != null) {
params = new ArrayList<NameValuePair>();
Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
try {
HttpPost post = new HttpPost(url);
post.addHeader("Accept-Encoding", "gzip");// 设置压缩
if (params != null) {
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
HttpClient client = createHttpClient(HTTP_CONNECTION_TIMEOUT, HTTP_SO_TIMEOUT);
HttpResponse httpResponse = client.execute(post);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = httpResponse.getEntity().getContent();
Header contenteCoding = httpResponse.getFirstHeader("content-Encoding");
if (contenteCoding != null && contenteCoding.getValue().equalsIgnoreCase("gzip")) {
inputStream = new GZIPInputStream(inputStream);// 解压
}
response = inputStreamToString(inputStream);
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
/**
* HttpClient-get
*/
public static String httpGet(String url, Map<String, String> map) {
String response = null;
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(url);
try {
if (map != null) {
urlBuilder.append("?");
Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
urlBuilder.append(URLEncoder.encode(entry.getKey(), HTTP.UTF_8));
urlBuilder.append("=");
urlBuilder.append(URLEncoder.encode(entry.getValue(), HTTP.UTF_8));
if (iterator.hasNext()) {
urlBuilder.append("&");
}
}
}
DefaultHttpClient httpClient = createHttpClient(HTTP_CONNECTION_TIMEOUT, HTTP_SO_TIMEOUT);
HttpGet httpGet = new HttpGet(urlBuilder.toString());
httpGet.addHeader("Accept-Encoding", "gzip");
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = httpResponse.getEntity().getContent();
Header contentCoding = httpResponse.getFirstHeader("Content-Encoding");
if (contentCoding != null && contentCoding.getValue().equalsIgnoreCase("gzip")) {
inputStream = new GZIPInputStream(inputStream);
}
response = inputStreamToString(inputStream);
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private static DefaultHttpClient createHttpClient(int connTimeout, int soTimeout) {
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, connTimeout);
HttpConnectionParams.setSoTimeout(httpParams, soTimeout);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
return httpClient;
}
private static String inputStreamToString(InputStream inputStream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, HTTP.UTF_8));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
}
DefaultHttpClient
最新推荐文章于 2020-05-26 12:01:28 发布