| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- package com.malk.hengyilong.utils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.*;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.UnknownHostException;
- import java.text.MessageFormat;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.Map.Entry;
- public class HTTPHelper {
- // slf4j日志记录器
- private static final Logger LOG = LoggerFactory.getLogger(HTTPHelper.class);
- /***
- * 向指定URL发送GET方法的请求
- *
- * @param apiUrl
- * @param data
- * @param projectId
- * @param signature
- * @param encoding
- * @return
- * @throws Exception
- */
- public static String sendGet(String apiUrl, HashMap<String, Object> param,
- LinkedHashMap<String, String> headers, String encoding) throws Exception {
- // 获得响应内容
- String http_RespContent = null;
- HttpURLConnection httpURLConnection = null;
- int http_StatusCode = 0;
- String http_RespMessage = null;
- try {
- // 实际请求完整Url
- StringBuffer fullUrl = new StringBuffer();
- if (null != param) {
- if (0 != param.size()) {
- StringBuffer params = new StringBuffer();
- for (Entry<String, Object> entry : param.entrySet()) {
- params.append(entry.getKey());
- params.append("=");
- params.append(entry.getValue().toString());
- params.append("&");
- }
- if (params.length() > 0) {
- params.deleteCharAt(params.lastIndexOf("&"));
- }
- fullUrl.append(apiUrl).append((params.length() > 0) ? "?" + params.toString() : "");
- } else {
- fullUrl.append(apiUrl);
- }
- } else {
- fullUrl.append(apiUrl);
- }
- LOG.info(">>>> 实际请求Url: " + fullUrl.toString());
- // 建立连接
- URL url = new URL(fullUrl.toString());
- httpURLConnection = (HttpURLConnection) url.openConnection();
- // 需要输出
- httpURLConnection.setDoOutput(true);
- // 需要输入
- httpURLConnection.setDoInput(true);
- // 不允许缓存
- httpURLConnection.setUseCaches(false);
- // HTTP请求方式
- httpURLConnection.setRequestMethod("GET");
- // 设置Headers
- if (null != headers) {
- for (String key : headers.keySet()) {
- httpURLConnection.setRequestProperty(key, headers.get(key));
- }
- }
- // 连接会话
- httpURLConnection.connect();
- // 获得响应状态(HTTP状态码)
- http_StatusCode = httpURLConnection.getResponseCode();
- // 获得响应消息(HTTP状态码描述)
- http_RespMessage = httpURLConnection.getResponseMessage();
- // 获得响应内容
- if (HttpURLConnection.HTTP_OK == http_StatusCode) {
- // 返回响应结果
- http_RespContent = getResponseContent(httpURLConnection);
- } else {
- // 返回非200状态时响应结果
- http_RespContent = getErrorResponseContent(httpURLConnection);
- String msg =
- MessageFormat.format("请求失败: Http状态码 = {0} , {1}", http_StatusCode, http_RespMessage);
- LOG.info(msg);
- }
- } catch (UnknownHostException e) {
- String message = MessageFormat.format("网络请求时发生异常: {0}", e.getMessage());
- Exception ex = new Exception(message);
- ex.initCause(e);
- throw ex;
- } catch (MalformedURLException e) {
- String message = MessageFormat.format("格式错误的URL: {0}", e.getMessage());
- Exception ex = new Exception(message);
- ex.initCause(e);
- throw ex;
- } catch (IOException e) {
- String message = MessageFormat.format("网络请求时发生异常: {0}", e.getMessage());
- Exception ex = new Exception(message);
- ex.initCause(e);
- throw ex;
- } catch (Exception e) {
- String message = MessageFormat.format("网络请求时发生异常: {0}", e.getMessage());
- Exception ex = new Exception(message);
- ex.initCause(e);
- throw ex;
- } finally {
- if (null != httpURLConnection) {
- httpURLConnection.disconnect();
- }
- }
- return http_RespContent;
- }
- /***
- * 向指定URL发送POST方法的请求
- *
- * @param apiUrl
- * @param data
- * @param projectId
- * @param signature
- * @param encoding
- * @return
- * @throws Exception
- */
- public static String sendPOST(String apiUrl, String data, LinkedHashMap<String, String> headers,
- String encoding) throws Exception {
- // 获得响应内容
- String http_RespContent = null;
- HttpURLConnection httpURLConnection = null;
- int http_StatusCode = 0;
- String http_RespMessage = null;
- try {
- // 建立连接
- URL url = new URL(apiUrl);
- httpURLConnection = (HttpURLConnection) url.openConnection();
- // 需要输出
- httpURLConnection.setDoOutput(true);
- // 需要输入
- httpURLConnection.setDoInput(true);
- // 不允许缓存
- httpURLConnection.setUseCaches(false);
- // HTTP请求方式
- httpURLConnection.setRequestMethod("POST");
- // 设置Headers
- if (null != headers) {
- for (String key : headers.keySet()) {
- httpURLConnection.setRequestProperty(key, headers.get(key));
- }
- }
- // 连接会话
- httpURLConnection.connect();
- // 建立输入流,向指向的URL传入参数
- DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
- // 设置请求参数
- dos.write(data.getBytes(encoding));
- dos.flush();
- dos.close();
- // 获得响应状态(HTTP状态码)
- http_StatusCode = httpURLConnection.getResponseCode();
- // 获得响应消息(HTTP状态码描述)
- http_RespMessage = httpURLConnection.getResponseMessage();
- // 获得响应内容
- if (HttpURLConnection.HTTP_OK == http_StatusCode) {
- // 返回响应结果
- http_RespContent = getResponseContent(httpURLConnection);
- } else {
- // 返回非200状态时响应结果
- http_RespContent = getErrorResponseContent(httpURLConnection);
- String msg =
- MessageFormat.format("请求失败: Http状态码 = {0} , {1}", http_StatusCode, http_RespMessage);
- LOG.info(msg);
- }
- } catch (UnknownHostException e) {
- String message = MessageFormat.format("网络请求时发生异常: {0}", e.getMessage());
- Exception ex = new Exception(message);
- ex.initCause(e);
- throw ex;
- } catch (MalformedURLException e) {
- String message = MessageFormat.format("格式错误的URL: {0}", e.getMessage());
- Exception ex = new Exception(message);
- ex.initCause(e);
- throw ex;
- } catch (IOException e) {
- String message = MessageFormat.format("网络请求时发生异常: {0}", e.getMessage());
- Exception ex = new Exception(message);
- ex.initCause(e);
- throw ex;
- } catch (Exception e) {
- String message = MessageFormat.format("网络请求时发生异常: {0}", e.getMessage());
- Exception ex = new Exception(message);
- ex.initCause(e);
- throw ex;
- } finally {
- if (null != httpURLConnection) {
- httpURLConnection.disconnect();
- }
- }
- return http_RespContent;
- }
- /***
- * 读取HttpResponse响应内容
- *
- * @param httpURLConnection
- * @return
- * @throws UnsupportedEncodingException
- * @throws IOException
- */
- private static String getResponseContent(HttpURLConnection httpURLConnection)
- throws UnsupportedEncodingException, IOException {
- StringBuffer contentBuffer = null;
- BufferedReader responseReader = null;
- try {
- contentBuffer = new StringBuffer();
- String readLine = null;
- responseReader =
- new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));
- while ((readLine = responseReader.readLine()) != null) {
- contentBuffer.append(readLine);
- }
- } finally {
- if (null != responseReader) {
- responseReader.close();
- }
- }
- return contentBuffer.toString();
- }
- /***
- * 读取HttpResponse响应内容
- *
- * @param httpURLConnection
- * @return
- * @throws UnsupportedEncodingException
- * @throws IOException
- */
- private static String getErrorResponseContent(HttpURLConnection httpURLConnection)
- throws UnsupportedEncodingException, IOException {
- StringBuffer contentBuffer = null;
- BufferedReader responseReader = null;
- try {
- contentBuffer = new StringBuffer();
- String readLine = null;
- responseReader =
- new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream(), "UTF-8"));
- while ((readLine = responseReader.readLine()) != null) {
- contentBuffer.append(readLine);
- }
- } finally {
- if (null != responseReader) {
- responseReader.close();
- }
- }
- return contentBuffer.toString();
- }
- }
|