日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HttpConnectionUtil

發(fā)布時間:2025/6/15 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HttpConnectionUtil 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘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=";/*** 構(gòu)造方法*/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之后的請求參數(shù)(注意:只對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,參數(shù):\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;}}

?

轉(zhuǎn)載于:https://my.oschina.net/u/576883/blog/733605

總結(jié)

以上是生活随笔為你收集整理的HttpConnectionUtil的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 人妻少妇精品无码专区二区 | 福利在线播放 | 在线观看黄网 | 一区二区av在线 | 国产精品人人人人 | 91午夜精品 | 色婷婷久久一区二区三区麻豆 | 欧美一级免费在线观看 | 精品日韩在线观看 | 亚洲v国产v| 中文有码一区 | 欧美成人免费大片 | 黄网站免费在线观看 | 贝利弗山的秘密在线观看 | 国内精品999 | 亚洲欧美中文日韩在线v日本 | 国内自拍第三页 | 少妇搡bbbb搡bbb搡打电话 | 欧美黑人性xxx猛交 少妇无套内谢久久久久 | 香蕉久久夜色精品国产使用方法 | 人妻天天爽夜夜爽一区二区三区 | 91在线播 | 懂色一区二区三区 | a级黄色网 | 波多野结衣乳巨码无在线 | 先锋影音中文字幕 | 一区二区三区有限公司 | n0659极腔濑亚美莉在线播放播放 | 口爆吞精一区二区三区 | 亚欧视频在线观看 | 黑人巨大精品欧美一区二区蜜桃 | 亚洲日本不卡 | 欧美国产高潮xxxx1819 | 亚洲一区二区在线观看视频 | 在线观看视频91 | 双性尿奴穿贞c带憋尿 | 国内露脸中年夫妇交换 | 91视频中文字幕 | www.国产在线 | 日韩高清免费av | 中文字幕资源站 | 亚洲三区在线 | 黄色大片在线看 | 少妇饥渴难耐 | 午夜h | 人人草人人草 | 97夜夜 | 成人里番精品一区二区 | 国产成人无码av | 日本午夜一区二区三区 | 国产黄色91 | 亚洲天堂av网站 | 久久av高潮av无码av喷吹 | 四虎网站在线 | 韩国美女啪啪 | 密臀av一区二区 | 亚洲欧美日韩图片 | 国产精品欧美性爱 | 国产精品videossex国产高清 | 91视频精选| 毛片官网 | 精品久久久久久久无码 | 中文字幕免费高清在线观看 | 美女试爆场恐怖电影在线观看 | 日韩成人午夜 | 亚洲精品字幕在线观看 | 国产精品一区二区毛片 | 伊人福利视频 | 黄色动漫在线观看 | 男人靠女人免费视频网站 | 亚洲七区 | 中国极品少妇videossexhd 就要干就要操 | 日本熟妇人妻xxxxx | 亚洲毛片精品 | 少妇喷水在线观看 | 亚洲成人高清在线观看 | 在线观看福利网站 | 伊人久久成人网 | 自拍偷拍第一页 | 性一交一乱一精一晶 | 国产精品88久久久久久妇女 | 中文字幕一区二区精品 | 浪荡奴双性跪着伺候 | 日韩欧美在线一区二区三区 | 中文字幕av播放 | a毛片在线 | 美女福利视频在线 | 亚洲欧美黄色片 | 免费在线观看黄网 | 亚洲一区中文 | 日韩激情网址 | 天天色影| 91av亚洲 | 秋霞福利网 | 汗汗视频 | 香蕉在线网站 | 国产熟女高潮视频 | 99riav国产| 最新国产在线视频 |