HttpUtil.java 2.8 KB
Newer Older
X
init  
xueli.xue 已提交
1
package com.xxl.job.client.util;
X
xueli.xue 已提交
2 3

import java.io.IOException;
X
xueli.xue 已提交
4 5 6 7 8
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
X
xueli.xue 已提交
9 10 11

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
X
xueli.xue 已提交
12
import org.apache.http.NameValuePair;
X
xueli.xue 已提交
13
import org.apache.http.client.config.RequestConfig;
X
xueli.xue 已提交
14 15
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
X
xueli.xue 已提交
16 17
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
X
xueli.xue 已提交
18
import org.apache.http.message.BasicNameValuePair;
X
xueli.xue 已提交
19 20 21
import org.apache.http.util.EntityUtils;

/**
X
xueli.xue 已提交
22
 * http util to send data
X
xueli.xue 已提交
23 24 25 26
 * @author xuxueli
 * @version  2015-11-28 15:30:59
 */
public class HttpUtil {
X
xueli.xue 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
	
	// response param
	public static final String status = "status";
	public static final String msg = "msg";
	// response status enum
	public static final String SUCCESS = "SUCCESS";
	public static final String FAIL = "FAIL";
	
	/**
	 * http post request
	 * @param reqURL
	 * @param params
	 * @return	[0]=responseMsg, [1]=exceptionMsg
	 */
	public static String[] post(String reqURL, Map<String, String> params){
		String responseMsg = null;
		String exceptionMsg = null;
		
		// do post
46 47
		HttpPost httpPost = null;
		CloseableHttpClient httpClient = null;
X
xueli.xue 已提交
48
		try{
49 50
			httpPost = new HttpPost(reqURL);
			httpClient = HttpClients.createDefault();
X
xueli.xue 已提交
51 52 53 54 55 56 57 58 59
			if (params != null && !params.isEmpty()) {
				List<NameValuePair> formParams = new ArrayList<NameValuePair>();
				for(Map.Entry<String,String> entry : params.entrySet()){
					formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
				}
				httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
			}
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
			httpPost.setConfig(requestConfig);
X
xueli.xue 已提交
60
			
X
xueli.xue 已提交
61
			HttpResponse response = httpClient.execute(httpPost);
X
xueli.xue 已提交
62 63
			HttpEntity entity = response.getEntity();
			if (null != entity) {
X
xueli.xue 已提交
64
				responseMsg = EntityUtils.toString(entity, "UTF-8");
X
xueli.xue 已提交
65 66
				EntityUtils.consume(entity);
			}
X
xueli.xue 已提交
67 68 69
			if (response.getStatusLine().getStatusCode() != 200) {
				exceptionMsg = "response.getStatusLine().getStatusCode() = " + response.getStatusLine().getStatusCode();
			}
X
xueli.xue 已提交
70
		} catch (Exception e) {
X
xueli.xue 已提交
71
			e.printStackTrace();
X
xueli.xue 已提交
72 73 74 75
			StringWriter out = new StringWriter();
			e.printStackTrace(new PrintWriter(out));
			exceptionMsg = out.toString();
		} finally{
76 77 78 79 80 81 82 83 84
			if (httpPost!=null) {
				httpPost.releaseConnection();
			}
			if (httpClient!=null) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
X
xueli.xue 已提交
85 86
			}
		}
X
xueli.xue 已提交
87 88 89 90 91
		
		String[] result = new String[2];
		result[0] = responseMsg;
		result[1] = exceptionMsg;
		return result;
X
xueli.xue 已提交
92 93
	}
}