---
title: "HttpClientUtil"
author: "xiaowei2002"
date: "2015-06-30T16:18:50Z"
tags: []
categories: ["Web前端"]
slug: "httpclientutil"
draft: false
lastmod: "2014-11-19T11:25:40Z"
id: 2157903
url: "https://xiaowei2002.iteye.com/blog/2157903"
---

/*
 * @(#)HttpClient.java 2013-7-30
 *
 * Copyright 2013 SZ-Gionee,Inc. All rights reserved.
 */
package com.yulong.util;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 * HTTP工具类
 * @author ZuoChangjun 2013-7-30
 */
public class HttpClientUtil {

	/**
	 * get方式请求
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String httpGet(String url) throws ClientProtocolException,
			IOException {
		HttpClient client = new DefaultHttpClient();
		// String
		// url="http://partner.appflood.com/partner_bid_get_token?app_key=XALn1GsPvvvfHfhn&duid=a100001bbc9a0f&d=%7B%22av%22%3A%22%22%2C%22br%22%3A%22%22%2C%22dn%22%3A%22%22%2C%22dp%22%3A%22%22%2C%22lc%22%3A%22%22%2C%22ll%22%3A%22%22%2C%22mc%22%3Afalse%2C%22mf%22%3A%22%22%2C%22pm%22%3A%22%22%2C%22pn%22%3A%22%22%2C%22sm%22%3A%22%22%2C%22sn%22%3A%22%22%2C%22so%22%3A%22%22%2C%22wc%22%3Afalse%2C%22xx%22%3A0%2C%22yy%22%3A0%7D";
		HttpGet get = new HttpGet(url);
		String result = null;
		try {
			HttpResponse res = client.execute(get);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = res.getEntity();
				result = EntityUtils.toString(entity, HTTP.UTF_8);
				// System.out.println(result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭连接 ,释放资源
			client.getConnectionManager().shutdown();
		}
		return result;

	}

	/** 
	 * post方式请求
	 * @param url
	 * @param jsonParams
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String httpPost(String url, String jsonParams)
			throws ClientProtocolException, IOException {

		HttpClient client = new DefaultHttpClient();
		// List<NameValuePair>list=new ArrayList<NameValuePair>();
		// list.add(new BasicNameValuePair("name", name));
		// list.add(new BasicNameValuePair("pwd", pwd));
		// httpPost.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));
		HttpPost post = new HttpPost(url);
		if(StringUtils.isNotBlank(jsonParams)){
			post.setEntity(new StringEntity(jsonParams));
		}
		String result = null;
		try {
			HttpResponse res = client.execute(post);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = res.getEntity();
				result = EntityUtils.toString(entity, HTTP.UTF_8);
				// System.out.println(result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭连接 ,释放资源
			client.getConnectionManager().shutdown();
		}
		return result;
	}
}