您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關HttpUtil工具類的參數是什么,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
調用方工具類及測試類,支持URL及JSON同時傳遞參數
package com.example.demo.util; 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.HttpPost; import org.apache.http.entity.StringEntity; 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; import org.apache.http.entity.ContentType; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; /** * Http調用工具類 * * @author zkyong * @date 2019-07-02 15:22:22 * @version v0.1 */ public class HttpUtil { public static final String UTF_8 = "UTF-8"; /** * 發送post請求 * * @param url URL地址 * @param socketTimeout 建立scoket連接超時時間 * @param connectTimeout 建立數據傳輸連接超時時間 * @param headers 請求頭參數 * @param params 請求參數 * @param json 請求體(json)參數 * @return String 響應報文 * @throws Exception 異常 */ public static String sendPost(String url, int socketTimeout, int connectTimeout, Map<String, String> headers, Map<String, ?> params, String json) throws Exception { // URL處理: 請求參數拼接到URL中傳遞 if (null != params && params.size() != 0) { List<NameValuePair> list = new ArrayList<>(); for (Entry<String, ?> entry : params.entrySet()) { list.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()))); } String uri = EntityUtils.toString(new UrlEncodedFormEntity(list, UTF_8)); if (uri != null) { url = url.contains("?") ? (url + "&" + uri) : (url + "?" + uri); } } // 構建請求: HttpPost HttpPost post = new HttpPost(url); RequestConfig config = RequestConfig.custom() .setSocketTimeout(socketTimeout) .setConnectTimeout(connectTimeout) .setConnectionRequestTimeout(connectTimeout) .build(); post.setConfig(config); // 設置請求頭: Header if (null != headers && headers.size() != 0) { for (Map.Entry<String, String> entry : headers.entrySet()) { post.addHeader(entry.getKey(), entry.getValue()); } } // 設置請求體: HttpEntity if (null != json && !"".equals(json.trim())) { post.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON)); } // 調用并返回結果 CloseableHttpClient client = null; CloseableHttpResponse response = null; try { client = HttpClients.createDefault(); response = client.execute(post); return EntityUtils.toString(response.getEntity(), UTF_8); } finally { if (response != null) { response.close(); } if (client != null) { client.close(); } } } }
package com.example.demo.util; import java.util.HashMap; import java.util.Map; import org.junit.Test; /** * Http調用工具測試類 * * @author zkyong * @date 2019-07-02 15:23:10 * @version v0.1 */ public class HttpUtilTest { @Test public void test01() throws Exception { String url = "http://localhost:8080/hello"; String json = "{\"hello\":\"wellcome\", \"name\":\"zhangsan\"}"; int socketTimeout = 120 * 1000; int connectTimeout = 120 * 1000; Map<String, String> params = new HashMap<>(); params.put("p1", "param01"); params.put("p2", "param02"); params.put("p3", "param03"); params.put("p4", "param04"); Map<String, String> headers = new HashMap<>(); headers.put("Content-Type", "application/json"); String response = HttpUtil.sendPost(url, socketTimeout, connectTimeout, headers, params, json); System.out.println("=================================================="); System.out.println(response); } }
被調用方:
package com.example.demo.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.example.demo.request.HelloDemoRequest; /** * HelloWorld Demo Web層 * * @author zkyong * @date 2019-07-02 15:26:21 * @version v0.1 */ @RestController public class HelloRestController { @RequestMapping(value = "/hello", method = RequestMethod.POST) public String hello(HttpServletRequest request, HttpServletResponse response, @RequestBody HelloDemoRequest body, String p1, String p2, String p3, String p4) { System.out.println("enter the hello method!"); return body.getHello() + " " + body.getName() + ", params : p1=" + p1 + ",p2=" + p2 + ",p3=" + p3 + ",p4=" + p4; } }
package com.example.demo.request; /** * HelloWorld Demo 請求類 * * @author zkyong * @date 2019-07-02 15:01:15 * @version v0.1 */ public class HelloDemoRequest { String hello; String name; public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "HelloDemoRequest [hello=" + hello + ", name=" + name + "]"; } }
關于HttpUtil工具類的參數是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。