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;
/**
 * 
 * 

类功能描述: HTTP请求工具类

*

类名:HttpUtil

*

创建时间:2018年1月9日 下午5:06:31

*

创建者:author

*/ @SuppressWarnings("deprecation") public class HttpUtil { private static Logger logger = Logger.getLogger(HttpUtil.class); /** * *

功能描述:get请求

*

方法名:doGet

*

@param url *

@return

*

返回类型:String

*

创建日期:2018年1月9日 下午4:38:39

*

创建者:author

*/ @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; } /** * *

功能描述:key-value 形式的参数请求

*

方法名:doPost

*

@param url *

@param grandParam *

@param authParam *

@param conditionParam *

@return

*

返回类型:String

*

创建日期:2018年1月9日 下午4:50:59

*

创建者:author

*/ @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 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); } }

你可能感兴趣的:(技术)