一、java常見實現http請求模板代碼
1、通過JDK網絡類Java.net.HttpURLConnection;
2、通過common封裝好的HttpClient;
3、通過Apache封裝好的CloseableHttpClient;
4、通過SpringBoot-RestTemplate;
5、socket
二、代碼
2.1、通過JDK網絡類Java.net.HttpURLConnection
比較原始的一種調用做法,這里把get請求和post請求都統一放在一個方法里面。
實現過程:
GET: 1、創建遠程連接 2、設置連接方式(get、post、put。。。) 3、設置連接超時時間 4、設置響應讀取時間 5、發起請求 6、獲取請求數據 7、關閉連接 POST: 1、創建遠程連接 2、設置連接方式(get、post、put。。。) 3、設置連接超時時間 4、設置響應讀取時間 5、當向遠程服務器傳送數據/寫數據時,需要設置為true(setDoOutput) 6、當前向遠程服務讀取數據時,設置為true,該參數可有可無(setDoInput) 7、設置傳入參數的格式:(setRequestProperty) 8、設置鑒權信息:Authorization:(setRequestProperty) 9、設置參數 10、發起請求 11、獲取請求數據 12、關閉連接
直接上代碼:
package com.riemann.springbootdemo.util.common.httpConnectionUtil; import org.springframework.lang.Nullable; import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; /** * @author riemann * @date 2019/05/24 23:42 */ public class HttpURLConnectionUtil { /** * Http get請求 * @param httpUrl 連接 * @return 響應數據 */ public static String doGet(String httpUrl){ //鏈接 HttpURLConnection connection = null; InputStream is = null; BufferedReader br = null; StringBuffer result = new StringBuffer(); try { //創建連接 URL url = new URL(httpUrl); connection = (HttpURLConnection) url.openConnection(); //設置請求方式 connection.setRequestMethod("GET"); //設置連接超時時間 connection.setReadTimeout(15000); //開始連接 connection.connect(); //獲取響應數據 if (connection.getResponseCode() == 200) { //獲取返回的數據 is = connection.getInputStream(); if (null != is) { br = new BufferedReader(new InputStreamReader(is, "UTF-8")); String temp = null; while (null != (temp = br.readLine())) { result.append(temp); } } } } catch (IOException e) { e.printStackTrace(); } finally { if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } //關閉遠程連接 connection.disconnect(); } return result.toString(); } /** * Http post請求 * @param httpUrl 連接 * @param param 參數 * @return */ public static String doPost(String httpUrl, @Nullable String param) { StringBuffer result = new StringBuffer(); //連接 HttpURLConnection connection = null; OutputStream os = null; InputStream is = null; BufferedReader br = null; try { //創建連接對象 URL url = new URL(httpUrl); //創建連接 connection = (HttpURLConnection) url.openConnection(); //設置請求方法 connection.setRequestMethod("POST"); //設置連接超時時間 connection.setConnectTimeout(15000); //設置讀取超時時間 connection.setReadTimeout(15000); //DoOutput設置是否向httpUrlConnection輸出,DoInput設置是否從httpUrlConnection讀入,此外發送post請求必須設置這兩個 //設置是否可讀取 connection.setDoOutput(true); connection.setDoInput(true); //設置通用的請求屬性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); //拼裝參數 if (null != param && param.equals("")) { //設置參數 os = connection.getOutputStream(); //拼裝參數 os.write(param.getBytes("UTF-8")); } //設置權限 //設置請求頭等 //開啟連接 //connection.connect(); //讀取響應 if (connection.getResponseCode() == 200) { is = connection.getInputStream(); if (null != is) { br = new BufferedReader(new InputStreamReader(is, "GBK")); String temp = null; while (null != (temp = br.readLine())) { result.append(temp); result.append("\r\n"); } } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { //關閉連接 if(br!=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } //關閉連接 connection.disconnect(); } return result.toString(); } public static void main(String[] args) { String message = doPost("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13026194071", ""); System.out.println(message); } }
運行結果:
2.2 通過apache common封裝好的HttpClient
httpClient的get或post請求方式步驟:
- 生成一個HttpClient對象並設置相應的參數;
- 生成一個GetMethod對象或PostMethod並設置響應的參數;
- 用HttpClient生成的對象來執行GetMethod生成的Get方法;
- 處理響應狀態碼;
- 若響應正常,處理HTTP響應內容;
- 釋放連接。
httpclinet3.1
導入如下jar包:
<!--HttpClient--> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.32</version> </dependency>
代碼如下:
package com.riemann.springbootdemo.util.common.httpConnectionUtil; import com.alibaba.fastjson.JSONObject; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import java.io.IOException; /** * @author riemann * @date 2019/05/25 0:58 */ public class HttpClientUtil { /** * httpClient的get請求方式 * 使用GetMethod來訪問一個URL對應的網頁實現步驟: * 1.生成一個HttpClient對象並設置相應的參數; * 2.生成一個GetMethod對象並設置響應的參數; * 3.用HttpClient生成的對象來執行GetMethod生成的Get方法; * 4.處理響應狀態碼; * 5.若響應正常,處理HTTP響應內容; * 6.釋放連接。 * @param url * @param charset * @return */ public static String doGet(String url, String charset) { //1.生成HttpClient對象並設置參數 HttpClient httpClient = new HttpClient(); //設置Http連接超時為5秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); //2.生成GetMethod對象並設置參數 GetMethod getMethod = new GetMethod(url); //設置get請求超時為5秒 getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000); //設置請求重試處理,用的是默認的重試處理:請求三次 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); String response = ""; //3.執行HTTP GET 請求 try { int statusCode = httpClient.executeMethod(getMethod); //4.判斷訪問的狀態碼 if (statusCode != HttpStatus.SC_OK) { System.err.println("請求出錯:" + getMethod.getStatusLine()); } //5.處理HTTP響應內容 //HTTP響應頭部信息,這里簡單打印 Header[] headers = getMethod.getResponseHeaders(); for(Header h : headers) { System.out.println(h.getName() + "---------------" + h.getValue()); } //讀取HTTP響應內容,這里簡單打印網頁內容 //讀取為字節數組 byte[] responseBody = getMethod.getResponseBody(); response = new String(responseBody, charset); System.out.println("-----------response:" + response); //讀取為InputStream,在網頁內容數據量大時候推薦使用 //InputStream response = getMethod.getResponseBodyAsStream(); } catch (HttpException e) { //發生致命的異常,可能是協議不對或者返回的內容有問題 System.out.println("請檢查輸入的URL!"); e.printStackTrace(); } catch (IOException e) { //發生網絡異常 System.out.println("發生網絡異常!"); } finally { //6.釋放連接 getMethod.releaseConnection(); } return response; } /** * post請求 * @param url * @param json * @return */ public static String doPost(String url, JSONObject json){ HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(url); postMethod.addRequestHeader("accept", "*/*"); postMethod.addRequestHeader("connection", "Keep-Alive"); //設置json格式傳送 postMethod.addRequestHeader("Content-Type", "application/json;charset=GBK"); //必須設置下面這個Header postMethod.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"); //添加請求參數 postMethod.addParameter("commentId", json.getString("commentId")); String res = ""; try { int code = httpClient.executeMethod(postMethod); if (code == 200){ res = postMethod.getResponseBodyAsString(); System.out.println(res); } } catch (IOException e) { e.printStackTrace(); } return res; } public static void main(String[] args) { System.out.println(doGet("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13026194071", "GBK")); System.out.println("-----------分割線------------"); System.out.println("-----------分割線------------"); System.out.println("-----------分割線------------"); JSONObject jsonObject = new JSONObject(); jsonObject.put("commentId", "13026194071"); System.out.println(doPost("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13026194071", jsonObject)); } }
運行結果:
httpclient4.5
maven
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.12</version> </dependency>
程序代碼:
package com.ming.http; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.*; import java.util.Map.Entry; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; 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.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class HttpClient4 { public static String doGet(String url) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; String result = ""; try { // 通過址默認配置創建一個httpClient實例 httpClient = HttpClients.createDefault(); // 創建httpGet遠程連接實例 HttpGet httpGet = new HttpGet(url); // 設置請求頭信息,鑒權 httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0"); // 設置配置請求參數 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 連接主機服務超時時間 .setConnectionRequestTimeout(35000)// 請求超時時間 .setSocketTimeout(60000)// 數據讀取超時時間 .build(); // 為httpGet實例設置配置 httpGet.setConfig(requestConfig); // 執行get請求得到返回對象 response = httpClient.execute(httpGet); // 通過返回對象獲取返回數據 HttpEntity entity = response.getEntity(); // 通過EntityUtils中的toString方法將結果轉換為字符串 result = EntityUtils.toString(entity); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 關閉資源 if (null != response) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != httpClient) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } public static String doPost(String url, Map<String, Object> paramMap) { CloseableHttpClient httpClient = null; CloseableHttpResponse httpResponse = null; String result = ""; // 創建httpClient實例 httpClient = HttpClients.createDefault(); // 創建httpPost遠程連接實例 HttpPost httpPost = new HttpPost(url); // 配置請求參數實例 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 設置連接主機服務超時時間 .setConnectionRequestTimeout(35000)// 設置連接請求超時時間 .setSocketTimeout(60000)// 設置讀取數據連接超時時間 .build(); // 為httpPost實例設置配置 httpPost.setConfig(requestConfig); // 設置請求頭 httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); // 封裝post請求參數 if (null != paramMap && paramMap.size() > 0) { List<NameValuePair> nvps = new ArrayList<NameValuePair>(); // 通過map集成entrySet方法獲取entity Set<Entry<String, Object>> entrySet = paramMap.entrySet(); // 循環遍歷,獲取迭代器 Iterator<Entry<String, Object>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Entry<String, Object> mapEntry = iterator.next(); nvps.add(new BasicNameValuePair(mapEntry.getKey(), mapEntry.getValue().toString())); } // 為httpPost設置封裝好的請求參數 try { httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } try { // httpClient對象執行post請求,並返回響應參數對象 httpResponse = httpClient.execute(httpPost); // 從響應對象中獲取響應內容 HttpEntity entity = httpResponse.getEntity(); result = EntityUtils.toString(entity); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 關閉資源 if (null != httpResponse) { try { httpResponse.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != httpClient) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } public static void main(String str[]){ System.out.println(HttpClient4.doGet("http://localhost:8080/test")); Map<String,Object> map=new HashMap<>(); map.put("name","ming"); System.out.println(HttpClient4.doPost("http://localhost:8080/testPost",map)); } }
post請求的jsonObject 的參數也成功寫入
2.3 通過Apache封裝好的CloseableHttpClient
CloseableHttpClient是在HttpClient的基礎上修改更新而來的,這里還涉及到請求頭token的設置(請求驗證),利用fastjson轉換請求或返回結果字符串為json格式,當然上面兩種方式也是可以設置請求頭token、json的,這里只在下面說明。
導入如下jar包:
<!--CloseableHttpClient--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.32</version> </dependency>
代碼如下:
package com.riemann.springbootdemo.util.common.httpConnectionUtil; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.io.UnsupportedEncodingException; /** * @author riemann * @date 2019/05/25 1:35 */ public class CloseableHttpClientUtil { private static String tokenString = ""; private static String AUTH_TOKEN_EXPIRED = "AUTH_TOKEN_EXPIRED"; private static CloseableHttpClient httpClient = null; /** * 以get方式調用第三方接口 * @param url * @param token * @return */ public static String doGet(String url, String token) { //創建HttpClient對象 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(url); if (null != tokenString && !tokenString.equals("")) { tokenString = getToken(); } //api_gateway_auth_token自定義header頭,用於token驗證使用 httpGet.addHeader("api_gateway_auth_token",tokenString); httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"); try { HttpResponse response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { //返回json格式 String res = EntityUtils.toString(response.getEntity()); return res; } } catch (IOException e) { e.printStackTrace(); } return null; } /** * 以post方式調用第三方接口 * @param url * @param json * @return */ public static String doPost(String url, JSONObject json) { if (null == httpClient) { httpClient = HttpClientBuilder.create().build(); } HttpPost httpPost = new HttpPost(url); if (null != tokenString && tokenString.equals("")) { tokenString = getToken(); } //api_gateway_auth_token自定義header頭,用於token驗證使用 httpPost.addHeader("api_gateway_auth_token", tokenString); httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"); try { StringEntity se = new StringEntity(json.toString()); se.setContentEncoding("UTF-8"); //發送json數據需要設置contentType se.setContentType("application/x-www-form-urlencoded"); //設置請求參數 httpPost.setEntity(se); HttpResponse response = httpClient.execute(httpPost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { //返回json格式 String res = EntityUtils.toString(response.getEntity()); return res; } } catch (IOException e) { e.printStackTrace(); } finally { if (httpClient != null){ try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } /** * 獲取第三方接口的token */ public static String getToken() { String token = ""; JSONObject object = new JSONObject(); object.put("appid", "appid"); object.put("secretkey", "secretkey"); if (null == httpClient) { httpClient = HttpClientBuilder.create().build(); } HttpPost httpPost = new HttpPost("http://localhost/login"); httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"); try { StringEntity se = new StringEntity(object.toString()); se.setContentEncoding("UTF-8"); //發送json數據需要設置contentType se.setContentType("application/x-www-form-urlencoded"); //設置請求參數 httpPost.setEntity(se); HttpResponse response = httpClient.execute(httpPost); //這里可以把返回的結果按照自定義的返回數據結果,把string轉換成自定義類 //ResultTokenBO result = JSONObject.parseObject(response, ResultTokenBO.class); //把response轉為jsonObject JSONObject result = (JSONObject) JSONObject.parseObject(String.valueOf(response)); if (result.containsKey("token")) { token = result.getString("token"); } } catch (IOException e) { e.printStackTrace(); } return token; } /** * 測試 */ public static void test(String telephone) { JSONObject object = new JSONObject(); object.put("telephone", telephone); //首先獲取token tokenString = getToken(); String response = doPost("http://localhost/searchUrl", object); //如果返回的結果是list形式的,需要使用JSONObject.parseArray轉換 //List<Result> list = JSONObject.parseArray(response, Result.class); System.out.println(response); } public static void main(String[] args) { test("12345678910"); } }
2.4 通過SpringBoot-RestTemplate
springBoot-RestTemple是上面三種方式的集大成者,代碼編寫更加簡單,目前可以采用的調用第三方接口有:
- delete() 在特定的URL上對資源執行HTTP DELETE操作
- exchange() 在URL上執行特定的HTTP方法,返回包含對象的ResponseEntity,這個對象是從響應體中映射得到的
- execute() 在URL上執行特定的HTTP方法,返回一個從響應體映射得到的對象
- getForEntity() 發送一個HTTP GET請求,返回的ResponseEntity包含了響應體所映射成的對象
- getForObject() 發送一個HTTP GET請求,返回的請求體將映射為一個對象
- postForEntity() POST 數據到一個URL,返回包含一個對象的ResponseEntity,這個對象是從響應體中映射得到的
- postForObject() POST 數據到一個URL,返回根據響應體匹配形成的對象
- headForHeaders() 發送HTTP HEAD請求,返回包含特定資源URL的HTTP頭
- optionsForAllow() 發送HTTP OPTIONS請求,返回對特定URL的Allow頭信息
- postForLocation() POST 數據到一個URL,返回新創建資源的URL
- put() PUT 資源到特定的URL
注意:目前標粗的為常用的
首先導入springboot的web包
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencies> <!--CloseableHttpClient--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <!--spring restTemplate--> <!-- @ConfigurationProperties annotation processing (metadata for IDEs) 生成spring-configuration-metadata.json類,需要引入此類--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
在啟動類同包下創建RestTemplateConfig.java類
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; /** * @author riemann * @date 2019/05/25 2:16 */ @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory){ return new RestTemplate(factory); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory(){ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setConnectTimeout(15000); factory.setReadTimeout(5000); return factory; } }
然后在Service類(RestTemplateToInterface )中注入使用
具體代碼如下:
import com.alibaba.fastjson.JSONObject; import com.swordfall.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; /** * @author riemann * @date 2019/05/25 2:20 */ @Service public class RestTemplateToInterface { @Autowired private RestTemplate restTemplate; /** * 以get方式請求第三方http接口 getForEntity * @param url * @return */ public User doGetWith1(String url){ ResponseEntity<User> responseEntity = restTemplate.getForEntity(url, User.class); User user = responseEntity.getBody(); return user; } /** * 以get方式請求第三方http接口 getForObject * 返回值返回的是響應體,省去了我們再去getBody() * @param url * @return */ public User doGetWith2(String url){ User user = restTemplate.getForObject(url, User.class); return user; } /** * 以post方式請求第三方http接口 postForEntity * @param url * @return */ public String doPostWith1(String url){ User user = new User("小白", 20); ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, user, String.class); String body = responseEntity.getBody(); return body; } /** * 以post方式請求第三方http接口 postForEntity * @param url * @return */ public String doPostWith2(String url){ User user = new User("小白", 20); String body = restTemplate.postForObject(url, user, String.class); return body; } /** * exchange * @return */ public String doExchange(String url, Integer age, String name){ //header參數 HttpHeaders headers = new HttpHeaders(); String token = "asdfaf2322"; headers.add("authorization", token); headers.setContentType(MediaType.APPLICATION_JSON); //放入body中的json參數 JSONObject obj = new JSONObject(); obj.put("age", age); obj.put("name", name); //組裝 HttpEntity<JSONObject> request = new HttpEntity<>(obj, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, String.class); String body = responseEntity.getBody(); return body; } }
2.5 Socket
使用JDK原生提供的net,無需其他jar包;
使用起來有點麻煩。
package socket; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.URLEncoder; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; public class SocketForHttpTest { private int port; private String host; private Socket socket; private BufferedReader bufferedReader; private BufferedWriter bufferedWriter; public SocketForHttpTest(String host,int port) throws Exception{ this.host = host; this.port = port; /** * http協議 */ // socket = new Socket(this.host, this.port); /** * https協議 */ socket = (SSLSocket)((SSLSocketFactory)SSLSocketFactory.getDefault()).createSocket(this.host, this.port); } public void sendGet() throws IOException{ //String requestUrlPath = "/z69183787/article/details/17580325"; String requestUrlPath = "/"; OutputStreamWriter streamWriter = new OutputStreamWriter(socket.getOutputStream()); bufferedWriter = new BufferedWriter(streamWriter); bufferedWriter.write("GET " + requestUrlPath + " HTTP/1.1\r\n"); bufferedWriter.write("Host: " + this.host + "\r\n"); bufferedWriter.write("\r\n"); bufferedWriter.flush(); BufferedInputStream streamReader = new BufferedInputStream(socket.getInputStream()); bufferedReader = new BufferedReader(new InputStreamReader(streamReader, "utf-8")); String line = null; while((line = bufferedReader.readLine())!= null){ System.out.println(line); } bufferedReader.close(); bufferedWriter.close(); socket.close(); } public void sendPost() throws IOException{ String path = "/"; String data = URLEncoder.encode("name", "utf-8") + "=" + URLEncoder.encode("張三", "utf-8") + "&" + URLEncoder.encode("age", "utf-8") + "=" + URLEncoder.encode("32", "utf-8"); // String data = "name=zhigang_jia"; System.out.println(">>>>>>>>>>>>>>>>>>>>>"+data); OutputStreamWriter streamWriter = new OutputStreamWriter(socket.getOutputStream(), "utf-8"); bufferedWriter = new BufferedWriter(streamWriter); bufferedWriter.write("POST " + path + " HTTP/1.1\r\n"); bufferedWriter.write("Host: " + this.host + "\r\n"); bufferedWriter.write("Content-Length: " + data.length() + "\r\n"); bufferedWriter.write("Content-Type: application/x-www-form-urlencoded\r\n"); bufferedWriter.write("\r\n"); bufferedWriter.write(data); bufferedWriter.write("\r\n"); bufferedWriter.flush(); BufferedInputStream streamReader = new BufferedInputStream(socket.getInputStream()); bufferedReader = new BufferedReader(new InputStreamReader(streamReader, "utf-8")); String line = null; while((line = bufferedReader.readLine())!= null) { System.out.println(line); } bufferedReader.close(); bufferedWriter.close(); socket.close(); } public static void main(String[] args) throws Exception { /** * http協議測試 */ //SocketForHttpTest forHttpTest = new SocketForHttpTest("www.baidu.com", 80); /** * https協議測試 */ SocketForHttpTest forHttpTest = new SocketForHttpTest("www.baidu.com", 443); try { forHttpTest.sendGet(); // forHttpTest.sendPost(); } catch (IOException e) { e.printStackTrace(); } } }
package com.riemann.springbootdemo.util.common.httpConnectionUtil;import com.alibaba.fastjson.JSONObject;import org.apache.commons.httpclient.*;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.params.HttpMethodParams;import java.io.IOException;/** * @author riemann * @date 2019/05/25 0:58 */publicclassHttpClientUtil{/** * httpClient的get請求方式 * 使用GetMethod來訪問一個URL對應的網頁實現步驟: * 1.生成一個HttpClient對象並設置相應的參數; * 2.生成一個GetMethod對象並設置響應的參數; * 3.用HttpClient生成的對象來執行GetMethod生成的Get方法; * 4.處理響應狀態碼; * 5.若響應正常,處理HTTP響應內容; * 6.釋放連接。 * @param url * @param charset * @return */publicstatic String doGet(String url, String charset){//1.生成HttpClient對象並設置參數 HttpClient httpClient =newHttpClient();//設置Http連接超時為5秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);//2.生成GetMethod對象並設置參數 GetMethod getMethod =newGetMethod(url);//設置get請求超時為5秒 getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,5000);//設置請求重試處理,用的是默認的重試處理:請求三次 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,newDefaultHttpMethodRetryHandler()); String response ="";//3.執行HTTP GET 請求try{int statusCode = httpClient.executeMethod(getMethod);//4.判斷訪問的狀態碼if(statusCode != HttpStatus.SC_OK){ System.err.println("請求出錯:"+ getMethod.getStatusLine());}//5.處理HTTP響應內容//HTTP響應頭部信息,這里簡單打印 Header[] headers = getMethod.getResponseHeaders();for(Header h : headers){ System.out.println(h.getName()+"---------------"+ h.getValue());}//讀取HTTP響應內容,這里簡單打印網頁內容//讀取為字節數組byte[] responseBody = getMethod.getResponseBody(); response =newString(responseBody, charset); System.out.println("-----------response:"+ response);//讀取為InputStream,在網頁內容數據量大時候推薦使用//InputStream response = getMethod.getResponseBodyAsStream();}catch(HttpException e){//發生致命的異常,可能是協議不對或者返回的內容有問題 System.out.println("請檢查輸入的URL!"); e.printStackTrace();}catch(IOException e){//發生網絡異常 System.out.println("發生網絡異常!");}finally{//6.釋放連接 getMethod.releaseConnection();}return response;}/** * post請求 * @param url * @param json * @return */publicstatic String doPost(String url, JSONObject json){ HttpClient httpClient =newHttpClient(); PostMethod postMethod =newPostMethod(url); postMethod.addRequestHeader("accept","*/*"); postMethod.addRequestHeader("connection","Keep-Alive");//設置json格式傳送 postMethod.addRequestHeader("Content-Type","application/json;charset=GBK");//必須設置下面這個Header postMethod.addRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");//添加請求參數 postMethod.addParameter("commentId", json.getString("commentId")); String res ="";try{int code = httpClient.executeMethod(postMethod);if(code ==200){ res = postMethod.getResponseBodyAsString(); System.out.println(res);}}catch(IOException e){ e.printStackTrace();}return res;}publicstaticvoidmain(String[] args){ System.out.println(doGet("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13026194071","GBK")); System.out.println("-----------分割線------------"); System.out.println("-----------分割線------------"); System.out.println("-----------分割線------------"); JSONObject jsonObject =newJSONObject(); jsonObject.put("commentId","13026194071"); System.out.println(doPost("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13026194071", jsonObject));}}