十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章将为大家详细讲解有关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, Mapheaders, Map params, String json) throws Exception { // URL处理: 请求参数拼接到URL中传递 if (null != params && params.size() != 0) { List list = new ArrayList<>(); for (Entry 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 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; Mapparams = new HashMap<>(); params.put("p1", "param01"); params.put("p2", "param02"); params.put("p3", "param03"); params.put("p4", "param04"); Map 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工具类的参数是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。