DefaultHttpClient

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


 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值