HttpClient请求工具类

package com.gfLoverChat.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

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.util.EntityUtils;
import org.apache.log4j.Logger;

import com.gfLoverChat.common.data.SystemAESValues;
import com.jick.common.encrytion.AESencytion;
/**
 * 
 * <p>类功能描述: HTTP请求工具类</p>
 * <p>类名:HttpUtil </p>
 * <p>创建时间:2018年1月9日 下午5:06:31 </p>
 * <p>创建者:author</p>
 */
@SuppressWarnings("deprecation")
public class HttpUtil {

	private static Logger logger = Logger.getLogger(HttpUtil.class);
	
	/**
	 * 
	 * <p>功能描述:get请求</p>
	 * <p>方法名:doGet</p>
	 * <p>@param url
	 * <p>@return</p>
	 * <p>返回类型:String</p>
	 * <p>创建日期:2018年1月9日 下午4:38:39</p>  
	 * <p>创建者:author</p>
	 */
	@SuppressWarnings("resource")
	public 	static String doGet(String url){
			String reStr = "";
			try{
				logger.info("---------------Get请求开始-------------");
				HttpClient client = new DefaultHttpClient();
				//发送get请求
				HttpGet request = new HttpGet(url);
				HttpResponse response = client.execute(request);
				
				//请求发送成功,并得到响应
				if(response.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK){
					//读取服务器返回过来的json字符串
					reStr = EntityUtils.toString(response.getEntity());
					logger.info("---------------Get请求成功-------------");
				}
				
			}catch(Exception e){
				logger.info("---------------异常信息-------------");
				e.printStackTrace();
			}
			return reStr;
	}
	
	/**
	 * 
	 * <p>功能描述:key-value 形式的参数请求</p>
	 * <p>方法名:doPost</p>
	 * <p>@param url
	 * <p>@param grandParam
	 * <p>@param authParam
	 * <p>@param conditionParam
	 * <p>@return</p>
	 * <p>返回类型:String</p>
	 * <p>创建日期:2018年1月9日 下午4:50:59</p>  
	 * <p>创建者:author</p>
	 */
	@SuppressWarnings("resource")
	public 	static String doPost(String url,String grandParam,String authParam,String conditionParam){
			BufferedReader in = null;
			String reStr = "";
			AESencytion aes = new AESencytion();
			try{
				logger.info("---------------Post请求开始-------------");
				//定义HttpClient
				HttpClient client = new DefaultHttpClient();
				//实例化HTTP方法
				HttpPost request = new HttpPost();
				request.setURI(new URI(url));
				
				//设置参数 -- 可根据具体需要对参数进行修改
				List<NameValuePair> nameValuePairs = new ArrayList<>();
				grandParam = aes.encrypt(grandParam, SystemAESValues.getAppAESKey(), SystemAESValues.getAppAESIparamter());
				authParam = aes.encrypt(authParam, SystemAESValues.getAppAESKey(), SystemAESValues.getAppAESIparamter());
				conditionParam = aes.encrypt(conditionParam, SystemAESValues.getAppAESKey(), SystemAESValues.getAppAESIparamter());
				nameValuePairs.add(new BasicNameValuePair("grandParam", grandParam));
				nameValuePairs.add(new BasicNameValuePair("authParam", authParam));
				nameValuePairs.add(new BasicNameValuePair("conditionParam", conditionParam));
				request.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
				
				HttpResponse response = client.execute(request);
				int code = response.getStatusLine().getStatusCode();
				if(code == 200){
					logger.info("---------------Post请求成功-------------");
					in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
					StringBuilder sb = new StringBuilder();
					String line = "";
					//换行符 与\n一致,只是屏蔽了Windows和Linux的区别,更保险一些
					String NL = System.getProperty("line.separator");
					while((line=in.readLine()) != null){
						sb.append(line+NL);
					}
					
					reStr = sb.toString();
					if(reStr != null){
						reStr = aes.decrypt(reStr, SystemAESValues.getAppAESKey(), SystemAESValues.getAppAESIparamter());
					}
				}else{
					System.out.println("错误状态码:"+code);
				}
				
			}catch(Exception e){
				logger.info("---------------异常信息-------------");
				e.printStackTrace();
			}finally{
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return reStr;
	}
	
	public static void main(String[] args) {
	
		//本地测试
//		String url = "http://localhost:8080/jkYHManagerSever/manager/member/selMemDayAnalyse.action";
//		String grandParam = "aaa";
//		String authParam = "201708172146215177449047";
//		String conditionParam = "{'orgid':'fa212f3adc2941cc821a9fa13f77dd62','addtime':'2017-12-10 - 2017-12-31'}";
//		String reStr = doPost(url, grandParam, authParam, conditionParam);
//		System.out.println(reStr);
		
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值