Java 發(fā)送http請(qǐng)求(get、post)的示例
1.情景展示
java發(fā)送get請(qǐng)求、post請(qǐng)求(form表單、json數(shù)據(jù))至另一服務(wù)器;
可設(shè)置HTTP請(qǐng)求頭部信息,可以接收服務(wù)器返回cookie信息,可以上傳文件等;
2.代碼實(shí)現(xiàn)
所需jar包:httpcore-4.4.1.jar;httpclient-4.4.1.jar;httpmime-4.4.1.jar;epoint-utils-9.3.3.jar
import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.security.GeneralSecurityException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPatch; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.util.EntityUtils; import com.epoint.core.utils.string.StringUtil; /** * HttpClient工具類,使用http-client包實(shí)現(xiàn),原先的common-httpclient已經(jīng)淘汰 * * @作者 ko * @version [版本號(hào), 2017年10月18日] */ public class HttpUtil { private static PoolingHttpClientConnectionManager connMgr; private static RequestConfig requestConfig; private static final int MAX_TIMEOUT = 7000; /** * 直接以流返回 */ public static final int RTN_TYPE_1 = 1; /** * 直接以string返回 */ public static final int RTN_TYPE_2 = 2; /** * 以map返回,reslut:接口結(jié)果string;statusCode:http狀態(tài)碼 */ public static final int RTN_TYPE_3 = 3; /** * 以map返回,reslut:接口結(jié)果string;statusCode:http狀態(tài)碼;cookie:response的cookie * cookie值鍵值對(duì),格式 key1=value1;key2=value2;... */ public static final int RTN_TYPE_4 = 4; /** * 默認(rèn)上傳文件的文件流或file 的key Name */ private static final String DEFAULT_BINARYBODY_KEYNAME = "file"; static { // 設(shè)置連接池 connMgr = new PoolingHttpClientConnectionManager(); // 設(shè)置連接池大小 connMgr.setMaxTotal(100); connMgr.setDefaultMaxPerRoute(connMgr.getMaxTotal()); // 在提交請(qǐng)求之前 測(cè)試連接是否可用 connMgr.setValidateAfterInactivity(1); RequestConfig.Builder configBuilder = RequestConfig.custom(); // 設(shè)置連接超時(shí) configBuilder.setConnectTimeout(MAX_TIMEOUT); // 設(shè)置讀取超時(shí) configBuilder.setSocketTimeout(MAX_TIMEOUT); // 設(shè)置從連接池獲取連接實(shí)例的超時(shí) configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT); requestConfig = configBuilder.build(); } /** * 發(fā)送 GET請(qǐng)求 * * @param apiUrl * API接口URL * @return String 響應(yīng)內(nèi)容 */ public static String doGet(String apiUrl) { return doHttp(apiUrl, null, "get", RTN_TYPE_2); } /** * 發(fā)送POST請(qǐng)求 * * @param apiUrl * API接口URL * @param params * K-V參數(shù) * @return String 響應(yīng)內(nèi)容 */ public static String doPost(String apiUrl, Map<String, Object> params) { return doHttp(apiUrl, params, "post", RTN_TYPE_2); } /** * 發(fā)送POST請(qǐng)求 * * @param apiUrl * API接口URL * @param json * json參數(shù) * @return String 響應(yīng)內(nèi)容 */ public static String doPostJson(String apiUrl, String json) { return doHttp(apiUrl, json, "post", RTN_TYPE_2); } /** * 發(fā)送 http 請(qǐng)求 * * @param apiUrl * API接口URL * @param params * {Map<String, Object> K-V形式、json字符串} * @param method * {null、或者post:POST請(qǐng)求、patch:PATCH請(qǐng)求、delete:DELETE請(qǐng)求、get:GET請(qǐng)求} * @param type * {HttpUtil.RTN_TYPE_1:請(qǐng)求返回stream(此時(shí)流需要在外部手動(dòng)關(guān)閉);HttpUtil. * RTN_TYPE_2:string;HttpUtil.RTN_TYPE_3:返回一個(gè)map,map包含結(jié)果( * 結(jié)果是string形式)以及http狀態(tài)碼;HttpUtil.RTN_TYPE_4:返回一個(gè)map,map包含結(jié)果( * 結(jié)果是string形式), http狀態(tài)碼和cookie;其他情況返回string} * 如果結(jié)果是個(gè)map,key為:result,statusCode,cookie,分別返回 結(jié)果 * string,http狀態(tài)碼,cookie; cookie值鍵值對(duì),格式 * key1=value1;key2=value2;... * @return stream或 string 或 map */ public static <T> T doHttp(String apiUrl, Object params, String method, int type) { return doHttp(apiUrl, null, params, method, type); } /** * 發(fā)送 http 請(qǐng)求 * * @param apiUrl * API接口URL * @param headerMap * header信息Map<String, String>,可設(shè)置cookie * @param params * {Map<String, Object> K-V形式、json字符串} * @param method * {null、或者post:POST請(qǐng)求、patch:PATCH請(qǐng)求、delete:DELETE請(qǐng)求、get:GET請(qǐng)求} * @param type * {HttpUtil.RTN_TYPE_1:請(qǐng)求返回stream(此時(shí)流需要在外部手動(dòng)關(guān)閉);HttpUtil. * RTN_TYPE_2:string;HttpUtil.RTN_TYPE_3:返回一個(gè)map,map包含結(jié)果( * 結(jié)果是string形式)以及http狀態(tài)碼;HttpUtil.RTN_TYPE_4:返回一個(gè)map,map包含結(jié)果( * 結(jié)果是string形式), http狀態(tài)碼和cookie;其他情況返回string} * 如果結(jié)果是個(gè)map,key為:result,statusCode,cookie,分別返回 結(jié)果 * string,http狀態(tài)碼,cookie; cookie值鍵值對(duì),格式 * key1=value1;key2=value2;... * @return stream或 string 或 map */ public static <T> T doHttp(String apiUrl, Map<String, String> headerMap, Object params, String method, int type) { CloseableHttpClient httpClient = null; if (isSSL(apiUrl)) { httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) .setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); } else { httpClient = HttpClients.createDefault(); } return doHttp(httpClient, apiUrl, headerMap, params, method, type); } /** * 發(fā)送 http 請(qǐng)求 * * @param httpClient * httpclient對(duì)象 由外部傳入,用戶 需要保持登錄狀態(tài)等情況 此時(shí)如果要ssl,那么要在外部加入ssl特性 * httpClient = * HttpClients.custom().setSSLSocketFactory(HttpUtil. * createSSLConnSocketFactory()) * .setConnectionManager(HttpUtil.getConnMgr()). * setDefaultRequestConfig(HttpUtil..getRequestConfig()).build(); * @param apiUrl * API接口URL * @param headerMap * header信息Map<String, String>,可設(shè)置cookie * * @param params * {Map<String, Object> K-V形式、json字符串} * @param method * {null、或者post:POST請(qǐng)求、patch:PATCH請(qǐng)求、delete:DELETE請(qǐng)求、get:GET請(qǐng)求} * @param type * {HttpUtil.RTN_TYPE_1:請(qǐng)求返回stream(此時(shí)流需要在外部手動(dòng)關(guān)閉);HttpUtil. * RTN_TYPE_2:string;HttpUtil.RTN_TYPE_3:返回一個(gè)map,map包含結(jié)果( * 結(jié)果是string形式)以及http狀態(tài)碼;HttpUtil.RTN_TYPE_4:返回一個(gè)map,map包含結(jié)果( * 結(jié)果是string形式), http狀態(tài)碼和cookie;其他情況返回string} * 如果結(jié)果是個(gè)map,key為:result,statusCode,cookie,分別返回 結(jié)果 * string,http狀態(tài)碼,cookie; cookie值鍵值對(duì),格式 * key1=value1;key2=value2;... * @return stream或 string 或 map */ @SuppressWarnings("unchecked") public static <T> T doHttp(CloseableHttpClient httpClient, String apiUrl, Map<String, String> headerMap, Object params, String method, int type) { HttpRequestBase httpPost = null; if (StringUtil.isNotBlank(method)) { if ("patch".equalsIgnoreCase(method)) { httpPost = new HttpPatch(apiUrl); } else if ("delete".equalsIgnoreCase(method)) { httpPost = new HttpDelete(apiUrl); } else if ("get".equalsIgnoreCase(method)) { httpPost = new HttpGet(apiUrl); } else if ("post".equalsIgnoreCase(method)) { httpPost = new HttpPost(apiUrl); } } else { httpPost = new HttpPost(apiUrl); } CloseableHttpResponse response = null; try { // 設(shè)置header信息 if (headerMap != null && !headerMap.isEmpty()) { for (Map.Entry<String, String> entry : headerMap.entrySet()) { httpPost.addHeader(entry.getKey(), entry.getValue()); } } if (isSSL(apiUrl)) { httpPost.setConfig(requestConfig); } // 參數(shù)不為null、要處理參數(shù) if (params != null) { // get請(qǐng)求拼接在url后面 if (httpPost instanceof HttpGet) { StringBuffer param = new StringBuffer(); if (params instanceof Map) { Map<String, Object> paramsConvert = (Map<String, Object>) params; int i = 0; for (String key : paramsConvert.keySet()) { if (i == 0) param.append("?"); else param.append("&"); param.append(key).append("=").append(paramsConvert.get(key)); i++; } } else { param.append("?" + params.toString()); } apiUrl += param; } // delete請(qǐng)求暫不處理 else if (!(httpPost instanceof HttpDelete)) { // K-V形式 if (params instanceof Map) { Map<String, Object> paramsConvert = (Map<String, Object>) params; List<NameValuePair> pairList = new ArrayList<>(paramsConvert.size()); for (Map.Entry<String, Object> entry : paramsConvert.entrySet()) { NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue() == null ? "" : entry.getValue().toString()); pairList.add(pair); } ((HttpEntityEnclosingRequestBase) httpPost) .setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8"))); } // json格式 else { StringEntity stringEntity = new StringEntity(params.toString(), "UTF-8"); stringEntity.setContentEncoding("UTF-8"); stringEntity.setContentType("application/json"); ((HttpEntityEnclosingRequestBase) httpPost).setEntity(stringEntity); } } } response = httpClient.execute(httpPost); // int statusCode = response.getStatusLine().getStatusCode(); // if (statusCode != HttpStatus.SC_OK) { // return null; // } HttpEntity entity = response.getEntity(); if (entity != null) { if (type == RTN_TYPE_1) { return (T) entity.getContent(); } else if (RTN_TYPE_2 == type) { return (T) EntityUtils.toString(entity, "UTF-8"); } else if (RTN_TYPE_3 == type || RTN_TYPE_4 == type) { Map<String, String> rtnMap = new HashMap<String, String>(); rtnMap.put("result", EntityUtils.toString(entity, "UTF-8")); rtnMap.put("statusCode", response.getStatusLine().getStatusCode() + ""); if (RTN_TYPE_4 == type) { rtnMap.put("cookie", getCookie(response)); } return (T) rtnMap; } else { return (T) EntityUtils.toString(entity, "UTF-8"); } } } catch (Exception e) { e.printStackTrace(); } finally { if (response != null && type != RTN_TYPE_1) { try { EntityUtils.consume(response.getEntity()); } catch (IOException e) { e.printStackTrace(); } } } return null; } /** * 上傳附件(post形式) * * @param url * 請(qǐng)求地址 * @param headerMap * header參數(shù)map Map<String, String> * @param paramMap * 額外的參數(shù)map,Map<String, String> * @param file * 可以選擇本地文件上傳;如果傳了file,又傳了fileName,那么文件名以fileName為準(zhǔn),否則 是file的文件名 * @param fileName * 以流傳輸時(shí),必須指定文件名 * @param ssl * 是否需要ssl * @return result,返回上傳結(jié)果,如果接口沒(méi)有返回值,則為狀態(tài)碼 */ public static String upload(String url, Map<String, String> headerMap, Map<String, String> paramMap, File file, String fileName, boolean ssl) { return upload(url, headerMap, paramMap, file, null, fileName, ssl); } /** * 上傳附件(post形式) * * @param url * 請(qǐng)求地址 * @param headerMap * header參數(shù)map Map<String, String> * @param paramMap * 額外的參數(shù)map,Map<String, String> * @param in * 文件流 * @param fileName * 以流傳輸時(shí),必須指定文件名 * @param ssl * 是否需要ssl * @return result,返回上傳結(jié)果,如果接口沒(méi)有返回值,則為狀態(tài)碼 */ public static String upload(String url, Map<String, String> headerMap, Map<String, String> paramMap, InputStream in, String fileName, boolean ssl) { return upload(url, headerMap, paramMap, null, in, fileName, ssl); } /** * 上傳附件(post形式) * * @param httpClient * 外部傳入httpClient * @param url * 請(qǐng)求地址 * @param headerMap * header參數(shù)map Map<String, String> * @param paramMap * 額外的參數(shù)map,Map<String, String> * @param file * 可以選擇本地文件上傳;如果傳了file,又傳了fileName,那么文件名以fileName為準(zhǔn),否則 是file的文件名 * @param fileName * 以流傳輸時(shí),必須指定文件名 * @param ssl * 是否需要ssl * @return result,返回上傳結(jié)果,如果接口沒(méi)有返回值,則為狀態(tài)碼 */ public static String upload(CloseableHttpClient httpClient, String url, Map<String, String> headerMap, Map<String, String> paramMap, File file, String fileName, boolean ssl) { return upload(httpClient, url, headerMap, paramMap, file, null, fileName, ssl); } /** * 上傳附件(post形式) * * @param httpClient * 外部傳入httpClient * @param url * 請(qǐng)求地址 * @param headerMap * header參數(shù)map Map<String, String> * @param paramMap * 額外的參數(shù)map,Map<String, String> * @param in * 文件流 * @param fileName * 以流傳輸時(shí),必須指定文件名 * @param ssl * 是否需要ssl * @return result,返回上傳結(jié)果,如果接口沒(méi)有返回值,則為狀態(tài)碼 */ public static String upload(CloseableHttpClient httpClient, String url, Map<String, String> headerMap, Map<String, String> paramMap, InputStream in, String fileName, boolean ssl) { return upload(httpClient, url, headerMap, paramMap, null, in, fileName, ssl); } /** * 上傳附件(post形式) * * @param url * 請(qǐng)求地址 * @param headerMap * header參數(shù)map Map<String, String> * @param paramMap * 額外的參數(shù)map,Map<String, String> * @param file * 可以選擇本地文件上傳,file,in互斥;如果傳了file,又傳了fileName,那么文件名以fileName為準(zhǔn),否則 * 是file的文件名 * @param in * 文件流 * @param fileName * 以流傳輸時(shí),必須指定文件名 * @param ssl * 是否需要ssl * @return result,返回上傳結(jié)果,如果接口沒(méi)有返回值,則為狀態(tài)碼 */ private static String upload(String url, Map<String, String> headerMap, Map<String, String> paramMap, File file, InputStream in, String fileName, boolean ssl) { CloseableHttpClient httpClient = null; if (ssl) { httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) .setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); } else { httpClient = HttpClients.createDefault(); } return upload(httpClient, url, headerMap, paramMap, file, in, fileName, ssl); } /** * 上傳附件(post形式) * * @param httpClient * 外部傳入httpClient * @param url * 請(qǐng)求地址 * @param headerMap * header參數(shù)map Map<String, String> * @param paramMap * 額外的參數(shù)map,Map<String, String> * @param file * 可以選擇本地文件上傳,file,in互斥;如果傳了file,又傳了fileName,那么文件名以fileName為準(zhǔn),否則 * 是file的文件名 * @param in * 文件流 * @param fileName * 以流傳輸時(shí),必須指定文件名 * @param ssl * 是否需要ssl * @return result,返回上傳結(jié)果,如果接口沒(méi)有返回值,則為狀態(tài)碼 */ private static String upload(CloseableHttpClient httpClient, String url, Map<String, String> headerMap, Map<String, String> paramMap, File file, InputStream in, String fileName, boolean ssl) { String result = ""; CloseableHttpResponse response = null; try { HttpPost httpPost = new HttpPost(url); // 設(shè)置header信息 if (headerMap != null && !headerMap.isEmpty()) { for (Map.Entry<String, String> entry : headerMap.entrySet()) { httpPost.addHeader(entry.getKey(), entry.getValue()); } } if (ssl) { httpPost.setConfig(requestConfig); } MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // 選擇以file形式上傳 if (file != null && file.exists()) { if (StringUtil.isNotBlank(fileName)) { builder.addBinaryBody(DEFAULT_BINARYBODY_KEYNAME, file, ContentType.DEFAULT_BINARY, fileName); } else { builder.addBinaryBody(DEFAULT_BINARYBODY_KEYNAME, file); } } // 以流上傳 else if (in != null && StringUtil.isNotBlank(fileName)) { builder.addBinaryBody(DEFAULT_BINARYBODY_KEYNAME, in, ContentType.DEFAULT_BINARY, fileName); } if (paramMap != null && !paramMap.isEmpty()) { for (Map.Entry<String, String> entry : paramMap.entrySet()) { builder.addPart(entry.getKey(), new StringBody(entry.getValue(), ContentType.TEXT_PLAIN)); } } HttpEntity reqEntity = builder.build(); httpPost.setEntity(reqEntity); response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity, "UTF-8"); } else { result = response.getStatusLine().getStatusCode() + ""; } } catch (Exception e) { e.printStackTrace(); } finally { if (response != null) { try { EntityUtils.consume(response.getEntity()); } catch (IOException e) { e.printStackTrace(); } } } return result; } private static String getCookie(HttpResponse httpResponse) { Map<String, String> cookieMap = new HashMap<String, String>(64); Header headers[] = httpResponse.getHeaders("Set-Cookie"); if (headers == null || headers.length == 0) { return null; } String cookie = ""; for (int i = 0; i < headers.length; i++) { cookie += headers[i].getValue(); if (i != headers.length - 1) { cookie += ";"; } } String cookies[] = cookie.split(";"); for (String c : cookies) { c = c.trim(); if (cookieMap.containsKey(c.split("=")[0])) { cookieMap.remove(c.split("=")[0]); } cookieMap.put(c.split("=")[0], c.split("=").length == 1 ? "" : (c.split("=").length == 2 ? c.split("=")[1] : c.split("=", 2)[1])); } String cookiesTmp = ""; for (String key : cookieMap.keySet()) { cookiesTmp += key + "=" + cookieMap.get(key) + ";"; } return cookiesTmp.substring(0, cookiesTmp.length() - 2); } /** * 創(chuàng)建SSL安全連接 * * @return */ public static SSLConnectionSocketFactory createSSLConnSocketFactory() { SSLConnectionSocketFactory sslsf = null; try { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); sslsf = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier() { @Override public boolean verify(String arg0, SSLSession arg1) { return true; } }); } catch (GeneralSecurityException e) { e.printStackTrace(); } return sslsf; } public static PoolingHttpClientConnectionManager getConnMgr() { return connMgr; } public static RequestConfig getRequestConfig() { return requestConfig; } private static boolean isSSL(String apiUrl) { if (apiUrl.indexOf("https") != -1 ) { return true; } else { return false; } } }
以上就是Java 發(fā)送http請(qǐng)求(get、post)的示例的詳細(xì)內(nèi)容,更多關(guān)于Java 發(fā)送http請(qǐng)求的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
手把手教你用Java實(shí)現(xiàn)一套簡(jiǎn)單的鑒權(quán)服務(wù)
現(xiàn)今大部分系統(tǒng)都會(huì)有自己的鑒權(quán)服務(wù),本文介紹了最常用的鑒權(quán)服務(wù),就是日常用戶的登錄登出,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05使用Lombok的@Builder注解帶來(lái)的兩大坑
這篇文章主要介紹了使用Lombok的@Builder注解帶來(lái)的兩大坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08基于Java實(shí)現(xiàn)一個(gè)復(fù)雜關(guān)系表達(dá)式過(guò)濾器
這篇文章主要為大家詳細(xì)介紹了如何基于Java實(shí)現(xiàn)一個(gè)復(fù)雜關(guān)系表達(dá)式過(guò)濾器。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-07-07深入理解Spring事務(wù)及傳播機(jī)制之原理解析與實(shí)際應(yīng)用
Spring事務(wù)管理機(jī)制提供了多種傳播行為,可以控制事務(wù)的范圍和隔離級(jí)別,保證數(shù)據(jù)一致性和完整性。在實(shí)際應(yīng)用中,需要根據(jù)具體業(yè)務(wù)場(chǎng)景選擇合適的傳播行為實(shí)現(xiàn)事務(wù)控制2023-04-04Java連接并操作Sedna XML數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Java連接并操作Sedna XML數(shù)據(jù)庫(kù)的方法,較為詳細(xì)的說(shuō)明了Sedna XML數(shù)據(jù)庫(kù)的原理與功能,并給出了基于java操作Sedna XML數(shù)據(jù)庫(kù)的方法,需要的朋友可以參考下2015-06-06SpringBoot配置線程池的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot配置線程池的實(shí)現(xiàn)示例,主要包括在Spring Boot中創(chuàng)建和配置線程池,包括設(shè)置線程池的大小、隊(duì)列容量、線程名稱等參數(shù),感興趣的可以了解一下2023-09-09Java超詳細(xì)教你寫(xiě)一個(gè)學(xué)籍管理系統(tǒng)案例
這篇文章主要介紹了怎么用Java來(lái)寫(xiě)一個(gè)學(xué)籍管理系統(tǒng),學(xué)籍管理主要涉及到學(xué)生信息的增刪查改,本篇將詳細(xì)的實(shí)現(xiàn),感興趣的朋友跟隨文章往下看看吧2022-03-03