您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)HttpUtil工具類的參數(shù)是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
調(diào)用方工具類及測(cè)試類,支持URL及JSON同時(shí)傳遞參數(shù)
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調(diào)用工具類 * * @author zkyong * @date 2019-07-02 15:22:22 * @version v0.1 */ public class HttpUtil { public static final String UTF_8 = "UTF-8"; /** * 發(fā)送post請(qǐng)求 * * @param url URL地址 * @param socketTimeout 建立scoket連接超時(shí)時(shí)間 * @param connectTimeout 建立數(shù)據(jù)傳輸連接超時(shí)時(shí)間 * @param headers 請(qǐng)求頭參數(shù) * @param params 請(qǐng)求參數(shù) * @param json 請(qǐng)求體(json)參數(shù) * @return String 響應(yīng)報(bào)文 * @throws Exception 異常 */ public static String sendPost(String url, int socketTimeout, int connectTimeout, Map<String, String> headers, Map<String, ?> params, String json) throws Exception { // URL處理: 請(qǐng)求參數(shù)拼接到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); } } // 構(gòu)建請(qǐng)求: HttpPost HttpPost post = new HttpPost(url); RequestConfig config = RequestConfig.custom() .setSocketTimeout(socketTimeout) .setConnectTimeout(connectTimeout) .setConnectionRequestTimeout(connectTimeout) .build(); post.setConfig(config); // 設(shè)置請(qǐng)求頭: Header if (null != headers && headers.size() != 0) { for (Map.Entry<String, String> entry : headers.entrySet()) { post.addHeader(entry.getKey(), entry.getValue()); } } // 設(shè)置請(qǐng)求體: HttpEntity if (null != json && !"".equals(json.trim())) { post.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON)); } // 調(diào)用并返回結(jié)果 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調(diào)用工具測(cè)試類 * * @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); } }
被調(diào)用方:
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 請(qǐng)求類 * * @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 + "]"; } }
關(guān)于HttpUtil工具類的參數(shù)是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。