2019独角兽企业重金招聘Python工程师标准>>>

package com.common.utils;/**-----------------------------------------------------------------* IBM Confidential** OCO Source Materials** WebSphere Commerce** (C) Copyright IBM Corp. 2011** The source code for this program is not published or otherwise* divested of its trade secrets, irrespective of what has* been deposited with the U.S. Copyright Office.*-----------------------------------------------------------------*/import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** *         Created on Nov 21, 2010* *         Http and Https connection utility.*/
public class HttpConnectionUtil {public static final String CLASS_NAME = HttpConnectionUtil.class.getName();/** The send method : POST. */public static final String SEND_METHOD_POST = "POST";/** The send method : GET. */public static final String SEND_METHOD_GET = "GET";/** The char set : UTF-8. */public static final String CHARSET_UTF8 = "UTF-8";/** The char set : GB2312. */public static final String CHARSET_GB2312 = "GB2312";/** The char set : GBK. */public static final String CHARSET_GBK = "GBK";/** 1 Second is equal to 1,000 millisecond. */public static final long MILLI_SECOND = 1000;public static final long DEFAULT_CONNECT_TIMEOUTSECONDS = 1;public static final long DEFAULT_READ_TIMEOUTSECONDS = 3;/** HTTP request property : HTTP1.1 JSON. */public static final String CONTENT_TYPE_APP_JSON = "application/json";/** HTTP request property : HTTP1.1 FORM. */public static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";/** HTTP request property : HTTP1.1 TEXT XML. */public static final String CONTENT_TYPE_TEXT_XML = "text/xml";/** HTTP request property : HTTP1.1 APPLICATION XML. */public static final String CONTENT_TYPE_APP_XML = "application/xml";private static final Logger LOGGER = LoggerFactory.getLogger(HttpConnectionUtil.class);/** The separator symbol. */private static final String SYMBOL_SEPERATOR = "&";/** The end line symbol. */private static final String SYMBOL_END_LINE = "\n";/** The request parameters symbol. */private static final String SYMBOL_REQUEST = "?";/** The blank string. */private static final String BLANK_STRING = "";/** The content type char set key. */private static final String CONTENT_TYPE_CHARSET_KEY = "charset=";/** The HTTP response code : OK */private static final int HTTP_OK = 200;/** HTTP request property : content type. */private static final String CONTENT_TYPE = "Content-type";/** HTTP request property : char set. */private static final String CONTENT_KEY_CHATSET = "; charset=";/*** 构造方法*/private HttpConnectionUtil() {}public static String sendGetHttpRequest(String sendUrl) throws HttpConnectionException {return sendGetHttpRequest(sendUrl, null, DEFAULT_CONNECT_TIMEOUTSECONDS, DEFAULT_READ_TIMEOUTSECONDS);}/*** Send the HTTP request and return the response string.* * @param sendUrl The request URL, it should begin with 'http://' or 'https://'. If postUrl is null, return is null.* @param sendParameters The request key-value parameters map. It could be null or empty.* @param method The HTTP send message method. For example 'POST', 'GET'. Default is POST.* @param charset The char set for coding and encoding string when send request and receive response. For example*            'GBK', 'UTF-8'. Default is UTF-8.* @param timeOutSeconds The connection time out in seconds. For example '10' means 10 seconds.* @return The response string.* @throws HttpConnectionException*/public static String sendGetHttpRequest(String sendUrl, String charset, long connetcTimeOutSeconds,long readTimeOutSeconds) throws HttpConnectionException {// validate send URL.if (sendUrl == null || sendUrl.length() == 0) {throw new HttpConnectionException("sendUrl不能为空");}String method = SEND_METHOD_GET;if (charset == null || charset.trim().length() == 0) {charset = CHARSET_UTF8;}if (connetcTimeOutSeconds == 0) {connetcTimeOutSeconds = DEFAULT_CONNECT_TIMEOUTSECONDS;}if (readTimeOutSeconds == 0) {readTimeOutSeconds = DEFAULT_READ_TIMEOUTSECONDS;}// send request.HttpURLConnection conn = null;String responseString = null;try {// build send message.String sendMessage = BLANK_STRING;// make URL.URL url = buildURL(sendUrl, sendMessage, method);// make HTTP connection.conn = (HttpURLConnection) url.openConnection();initConnection(conn, method, charset, connetcTimeOutSeconds * MILLI_SECOND, readTimeOutSeconds* MILLI_SECOND);// send request.conn.connect();if (method != null && SEND_METHOD_POST.equals(method) && sendMessage != null) {OutputStream outStrm = conn.getOutputStream();DataOutputStream out = new DataOutputStream(outStrm);out.writeBytes(sendMessage);out.flush();out.close();}// build response string.int respCode = conn.getResponseCode();String responseCharset = conn.getContentType().substring(conn.getContentType().indexOf(CONTENT_TYPE_CHARSET_KEY) + CONTENT_TYPE_CHARSET_KEY.length());InputStream inStream = null;if (respCode != HTTP_OK) {// inStream = conn.getErrorStream();// String responseMessage = readInputStream(inStream, responseCharset);throw new HttpConnectionException("Http connection fail : [" + respCode + "] "+ conn.getResponseMessage());} else {inStream = conn.getInputStream();}responseString = readInputStream(inStream, responseCharset);} catch (IOException e) {throw new HttpConnectionException("IOException异常", e);} catch (Exception e) {throw new HttpConnectionException("Exception异常", e);} finally {if (conn != null) {conn.disconnect();}}return responseString;}/*** Read the input stream and build the message string.* * @param inStream* @param charset* @return* @throws HttpConnectionException*/private static String readInputStream(InputStream inputStream, String charset) throws HttpConnectionException {String response = null;if (inputStream == null) {throw new HttpConnectionException("Error Input Stream Parameter: null");}try {BufferedInputStream bufferedinputstream = new BufferedInputStream(inputStream);InputStreamReader isr = new InputStreamReader(bufferedinputstream, charset);BufferedReader in = new BufferedReader(isr);StringBuffer buff = new StringBuffer();String readLine = null;String endLine = SYMBOL_END_LINE;while ((readLine = in.readLine()) != null) {buff.append(readLine).append(endLine);}if (buff.length() > 0) {response = buff.substring(0, buff.length() - 1);} else {response = buff.toString();}} catch (UnsupportedEncodingException e) {throw new HttpConnectionException("Unsupported Encoding Exception: " + e);} catch (IOException e) {throw new HttpConnectionException("IOException: " + e);}return response;}/*** Build URL for POST and GET method.* * @param URL* @param message* @param method* @return* @throws HttpConnectionException*/private static URL buildURL(String URL, String message, String method) throws HttpConnectionException {URL url = null;if (SEND_METHOD_GET.equals(method)) {if (URL.indexOf(SYMBOL_REQUEST) > -1) {try {url = new URL(URL + SYMBOL_SEPERATOR + message);} catch (MalformedURLException e) {throw new HttpConnectionException("Create URL Error: " + e);}} else {try {url = new URL(URL + SYMBOL_REQUEST + message);} catch (MalformedURLException e) {throw new HttpConnectionException("Create URL Error: " + e);}}} else if (SEND_METHOD_POST.equals(method)) {try {url = new URL(URL);} catch (MalformedURLException e) {throw new HttpConnectionException("Create URL Error: " + e);}} else {throw new HttpConnectionException("Error Send Method Parameter: " + method);}return url;}/*** Initial the connection for POST and GET method.* * @param connection* @param method* @param charset* @param timeOut* @throws HttpConnectionException*/private static void initConnection(HttpURLConnection connection, String method, String charset, Long connetTimeOut,Long readTimeOut) throws HttpConnectionException {if (connection == null) {throw new HttpConnectionException("Error HttpURLConnection Parameter: null");}connection.setUseCaches(false);connection.setDefaultUseCaches(false);connection.setDoInput(true);if (method != null && method.trim().length() > 0) {try {connection.setRequestMethod(method);} catch (ProtocolException e) {throw new HttpConnectionException("Set Request Method Error: " + e);}} else {throw new HttpConnectionException("Error Method Parameter: " + method);}if (SEND_METHOD_POST.equals(method)) {connection.setDoOutput(true);if (charset != null && charset.trim().length() > 0) {connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=" + charset);} else {throw new HttpConnectionException("Error Charset Parameter: " + charset);}}if (connetTimeOut != null && readTimeOut != null) {connection.setConnectTimeout(connetTimeOut.intValue());connection.setReadTimeout(readTimeOut.intValue());}connection.setRequestProperty("User-Agent", "Mozilla/MSIE");}/*** Send the HTTP request and return the response string.* * @param sendUrl The request URL, it should begin with 'http://' or 'https://'. The sendUrl should not be null.* @param sendMessage The message for send. It could be null or empty.* @return The response string.* @throws IOException* @throws HttpConnectionException*/public static String sendPostHttpRequest(String sendUrl, String sendMessage) throws IOException,HttpConnectionException {return sendHttpRequest(sendUrl, sendMessage, CONTENT_TYPE_APP_JSON, SEND_METHOD_POST, CHARSET_UTF8,DEFAULT_CONNECT_TIMEOUTSECONDS, DEFAULT_READ_TIMEOUTSECONDS);}/*** form表单形式请求* * @param sendUrl post请求的URL* @param encodeParamters Encode之后的请求参数(注意:只对key和value进行encode,不要对等号进行encode),例如:key1=value1&key2=value2* @return* @throws IOException* @throws HttpConnectionException* @since 10.0*/public static String sendPostFormHttpRequest(String sendUrl, String encodeParamters) throws IOException,HttpConnectionException {return sendHttpRequest(sendUrl, encodeParamters, CONTENT_TYPE_FORM, SEND_METHOD_POST, CHARSET_UTF8,DEFAULT_CONNECT_TIMEOUTSECONDS, DEFAULT_READ_TIMEOUTSECONDS);}/*** Send the HTTP request and return the response string.* * @param sendUrl The request URL, it should begin with 'http://' or 'https://'. The sendUrl should not be null.* @param sendMessage The message for send. It could be null or empty.* @param contentType The content type of HTTP request head. The contentType should not be null.* @param method The HTTP send message method. For example 'POST', 'GET'. Default is POST.* @param charset The char set for coding and encoding string when send request and receive response. For example*            'GBK', 'UTF-8'. Default is UTF-8.* @return The response string.* @throws IOException* @throws HttpConnectionException*/public static String sendHttpRequest(String sendUrl, String sendMessage, String contentType, String method,String charset) throws IOException, HttpConnectionException {return sendHttpRequest(sendUrl, sendMessage, contentType, method, charset, DEFAULT_CONNECT_TIMEOUTSECONDS,DEFAULT_READ_TIMEOUTSECONDS);}/*** Send the HTTP request and return the response string.* * @param sendUrl The request URL, it should begin with 'http://' or 'https://'. The sendUrl should not be null.* @param sendMessage The message for send. It could be null or empty.* @param contentType The content type of HTTP request head. The contentType should not be null.* @param method The HTTP send message method. For example 'POST', 'GET'. Default is POST.* @param charset The char set for coding and encoding string when send request and receive response. For example*            'GBK', 'UTF-8'. Default is UTF-8.* @param connectTimeOutSeconds The connect time out in seconds. For example '10' means 10 seconds.* @param readTimeOutSeconds The read time out in seconds. For example '10' means 10 seconds.* @return The response string.* @throws IOException* @throws HttpConnectionException*/public static String sendHttpRequest(String sendUrl, String sendMessage, String contentType, String method,String charset, long connectTimeOutSeconds, long readTimeOutSeconds) throws IOException,HttpConnectionException {// validate send URL.if (sendUrl == null || sendUrl.length() == 0) {throw new IOException("Request param error : sendUrl is null.");}if (method == null || method.trim().length() == 0) {if (sendMessage == null || sendMessage.trim().length() == 0) {method = SEND_METHOD_GET;} else {method = SEND_METHOD_POST;}}if (SEND_METHOD_POST.equals(method) && (sendMessage == null || sendMessage.length() == 0)) {throw new IOException("Request param error : sendMessage is null.");}if (contentType == null || contentType.length() == 0) {throw new IOException("Request param error : contentType is null.");}if (charset == null || charset.trim().length() == 0) {charset = CHARSET_UTF8;}// send request.HttpURLConnection conn = null;String responseString = null;Map<String, List<String>> responseHeader = null;try {LOGGER.info("sendHttpRequest,参数:\n{}", sendMessage);// make URL.URL url = buildURL(sendUrl, sendMessage, method);// make HTTP connection.conn = (HttpURLConnection) url.openConnection();initConnection(conn, method, contentType, charset, Long.valueOf(connectTimeOutSeconds * MILLI_SECOND),Long.valueOf(readTimeOutSeconds * MILLI_SECOND));// send request.conn.connect();if (method != null && SEND_METHOD_POST.equals(method) && sendMessage != null) {OutputStream outStrm = conn.getOutputStream();DataOutputStream out = new DataOutputStream(outStrm);out.write(sendMessage.getBytes(charset));out.flush();out.close();}// build response string.int respCode = conn.getResponseCode();String responseCharset = charset;if (conn.getContentType() != null && conn.getContentType().indexOf(CONTENT_TYPE_CHARSET_KEY) > 0) {responseCharset = conn.getContentType().substring(conn.getContentType().indexOf(CONTENT_TYPE_CHARSET_KEY) + CONTENT_TYPE_CHARSET_KEY.length());responseCharset = formatCharset(responseCharset);}responseHeader = conn.getHeaderFields();InputStream inStream = null;if (respCode != HTTP_OK) {inStream = conn.getErrorStream();String responseMessage = readInputStream(inStream, responseCharset);throw new IOException("Http connection fail : [" + respCode + "] " + conn.getResponseMessage() + "\n"+ responseMessage);} else {inStream = conn.getInputStream();responseString = readInputStream(inStream, responseCharset);}} catch (IOException e) {throw new IOException("IOException: " + e + "\nURL = " + sendUrl + "\nParameter = "+ sendMessage.toString() + "\nResponse Header = " + responseHeader);} finally {if (conn != null) {conn.disconnect();}}return responseString;}/*** Initial the connection for POST and GET method.* * @param connection* @param method* @param contentType* @param charset* @param connectTimeOut* @param readTimeOut* @throws IOException*/private static void initConnection(HttpURLConnection connection, String method, String contentType, String charset,Long connectTimeOut, Long readTimeOut) throws IOException {Map<String, String> properties = new HashMap<String, String>();if (method != null && SEND_METHOD_POST.equals(method)) {if (contentType != null && contentType.trim().length() > 0) {properties.put(CONTENT_TYPE, contentType);if (charset != null && charset.trim().length() > 0) {properties.put(CONTENT_TYPE, properties.get(CONTENT_TYPE) + CONTENT_KEY_CHATSET + charset);}}}initConnection(connection, method, properties, connectTimeOut, readTimeOut);}/*** Initial the connection for POST and GET method.* * @param connection* @param method* @param requestProperties* @param connectTimeOut* @param readTimeOut* @throws IOException*/private static void initConnection(HttpURLConnection connection, String method,Map<String, String> requestProperties, Long connectTimeOut, Long readTimeOut) throws IOException {if (connection != null) {connection.setUseCaches(false);connection.setDefaultUseCaches(false);connection.setDoInput(true);if (method != null && method.trim().length() > 0) {try {connection.setRequestMethod(method);} catch (ProtocolException e) {throw new IOException("Set Request Method Error: " + e);}if (SEND_METHOD_POST.equals(method)) {connection.setDoOutput(true);}} else {throw new IOException("Error Method Parameter: " + method);}if (requestProperties != null && requestProperties.size() > 0) {String key, value;for (Map.Entry<String, String> tempEntry : requestProperties.entrySet()) {key = tempEntry.getKey();value = tempEntry.getValue();connection.setRequestProperty(key, value);}}if (connectTimeOut != null && readTimeOut != null) {System.setProperty("sun.net.client.defaultConnectTimeout", connectTimeOut.toString());System.setProperty("sun.net.client.defaultReadTimeout", readTimeOut.toString());connection.setConnectTimeout(connectTimeOut.intValue());connection.setReadTimeout(readTimeOut.intValue());}connection.setRequestProperty("User-Agent", "Mozilla/MSIE");} else {throw new IOException("Error HttpURLConnection Parameter connection is null.");}}/** Format the char-set string. */private static String formatCharset(String responseCharset) {String result = responseCharset;if (result != null && result.length() > 0) {result = result.trim();result = result.replaceAll(";", "");}return result;}}

转载于:https://my.oschina.net/u/576883/blog/733605

HttpConnectionUtil相关推荐

  1. android http通过post请求发送一个xml

    今天,简单讲讲android如何在网络请求时通过post方式发送xml数据. 其实也很简单,不过我之前对网络请求这一块不太熟悉,当需要做这个发送xml数据时,居然不知道怎么做.后来,在网上查找资料,最 ...

  2. java http 下载文件_JAVA通过HttpURLConnection 上传和下载文件的方法

    本文介绍了JAVA通过HttpURLConnection 上传和下载文件的方法,分享给大家,具体如下: HttpURLConnection文件上传 HttpURLConnection采用模拟浏览器上传 ...

  3. 对InputStreamReader 和 OutputStreamWriter的理解

    一.InputStreamReader类 InputStreamReader 将字节流转换为字符流.是字节流通向字符流的桥梁.如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK. 构 ...

  4. 对HTTPCONNECTION的理解

    首先写一个工具类HttpConnection package com.example.pc.httpconnectiontest;import java.io.BufferedReader; impo ...

  5. android 拼接参数,Android 多参数多文件同时上传

    本类可以实现在安卓开发中在一个表单中同时提交参数.数据和上传多个文件,并可以自行制定字段名称.直接上代码: package com.example.myuploadtest.utils; import ...

  6. java的connect和http_【JAVA】通过URLConnection/HttpURLConnection发送HTTP请求的方法

    Java原生的API可用于发送HTTP请求 即java.net.URL.java.net.URLConnection,JDK自带的类: 1.通过统一资源定位器(java.net.URL)获取连接器(j ...

  7. Android向:实现同一局域网内两台手机之间的文件互传

    背景 最近要做一个demo,目的是实现局域网内的两台手机之间的文件互传.具体流程如下: 手机 A 从服务器上下载一个 apk 文件到本机上: 手机 A 在自己的某个端口上启动一个 Server 服务, ...

  8. java 通联支付接口_allinpay 通联支付接口实例

    [实例简介] allinpay 支付的实例代码,这只是部分,需要其它的请联系我. 帮忙找 [实例截图] [核心代码] 201708081652114811 └── unionorder_demo ├─ ...

  9. java 通联支付接口_通联支付接口.rar

    1 通联接口\unionorder_demo\java\uniondemo\.classpath 841 Bytes 2018/3/19 11:18:42 2 通联接口\unionorder_demo ...

最新文章

  1. glog 报错解决: /bin/bash: aclocal-1.14: command not found
  2. 站在吃货的角度来解释那些和微服务有关的名词
  3. stm32 pc13~pc15 tamper-rtc OSC32-IN/OSC32-OUT 配置成IO口
  4. 何时查询2021高考成绩长春市,2020年吉林长春成人高考成绩查询入口(已开通)...
  5. Cortex-M3-建立堆栈
  6. 【CodeForces - 1038B 】Non-Coprime Partition (构造,数组特征)
  7. LeetCode 1013. 将数组分成和相等的三个部分
  8. LFFD 再升级!新增行人和人头检测模型,还有了优化的C++实现
  9. 软件加license的一种实现方法
  10. c语言编辑学生信息录入的程序,c语言编的学生信息管理系统小程序!!有不足的请指出,谢谢!!...
  11. Java编程思想学习录(连载之:初始化与清理)
  12. 计算机应用基础教程清华大学,清华大学出版社-图书详情-《大学计算机应用基础教程(第3版)》...
  13. 微信小程序项目实例——投骰子
  14. dos命令行的四种打开方式
  15. 手机上的环境光传感器
  16. 面试题10:青蛙跳台阶
  17. Python CGI编程实现网页上传本地文件
  18. opencv抠出圆形区域_用OpenCV检测圆形区域(包含大量小对象)
  19. centos搭建流媒体服务器
  20. NLP中<SOS>、<EOS>、<UNK>、<PAD>等标识符的含义

热门文章

  1. 几个极品笑话,放松下心情
  2. python cx_oracle_Python3安装cx_Oracle连接oracle数据库实操总结
  3. Windows Pe 第三章 PE头文件(下)
  4. 【组合数学】排列组合 ( 多重集组合数 | 所有元素重复度大于组合数 | 多重集组合数 推导 1 分割线推导 | 多重集组合数 推导 2 不定方程非负整数解个数推导 )
  5. 【集合论】二元关系 ( 定义域 | 值域 | 域 | 逆运算 | 逆序合成运算 | 限制 | 像 | 单根 | 单值 | 合成运算的性质 )
  6. 【Android 内存优化】内存抖动 ( 垃圾回收算法总结 | 分代收集算法补充 | 内存抖动排查 | 内存抖动操作 | 集合选择 )
  7. 【嵌入式开发】C语言 指针数组 多维数组
  8. MongonDB 知识
  9. 前端学习笔记day01 html 标签之音频 embed+audio+video
  10. BZOJ 3884 上帝与集合的正确用法 (欧拉定理)