分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

HTTPUTILS

发布时间:2023-09-06 02:28责任编辑:白小东关键词:暂无标签

maven依赖

<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.6</version></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency>

  

工具类

package com.yyjdemo.shardingjdbc.http;import com.alibaba.fastjson.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;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.client.utils.URIBuilder;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.params.HttpParams;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class HttpUtils { ???public static void main(String[] args) throws IOException { ???????JSONObject jsonObject = new JSONObject(); ???????jsonObject.put("id","1");// ???????String httppost = httppost("http://127.0.0.1:8080/jdbc/testpost",jsonObject);// ???????System.out.println(httppost);// ???????String httppost = httppostjson("http://127.0.0.1:8080/jdbc/testpostjson",jsonObject); ???????String httppost = httpget("http://127.0.0.1:8080/jdbc/test",jsonObject); ???} ???public static String httpget(String url, JSONObject jsonObject) { ???????try { ???????????URIBuilder uriBuilder = new URIBuilder(url); ???????????if(!jsonObject.isEmpty()){ ???????????????jsonObject.forEach((k,v)->{ ???????????????????uriBuilder.addParameter(k,v.toString()); ???????????????}); ???????????} ???????????HttpGet httpget = new HttpGet(uriBuilder.toString()); ???????????try(CloseableHttpClient httpclient = HttpClients.createDefault(); ???????????????CloseableHttpResponse execute = httpclient.execute(httpget);){ ???????????????HttpEntity entity = execute.getEntity(); ???????????????String s = EntityUtils.toString(entity); ???????????????return s; ???????????} ???????}catch (Exception e){ ???????????e.printStackTrace(); ???????} ????return null; ???} ???public static String httppostjson(String url, JSONObject jsonObject) { ???????try { ???????????HttpPost httpPost = new HttpPost(url); ???????????httpPost.addHeader(HTTP.CONTENT_TYPE,"application/json"); ???????????StringEntity stringEntity = new StringEntity(jsonObject.toString(),"utf-8"); ???????????httpPost.setEntity(stringEntity); ???????????try(CloseableHttpClient httpclient = HttpClients.createDefault(); ???????????????CloseableHttpResponse execute = httpclient.execute(httpPost);){ ???????????????HttpEntity entity = execute.getEntity(); ???????????????String s = EntityUtils.toString(entity); ???????????????return s; ???????????} ???????}catch (Exception e){ ???????????e.printStackTrace(); ???????} ???????return null; ???} ???private static String httppost(String url,JSONObject jsonObject){ ???????try { ???????????HttpPost httpPost = new HttpPost(url); ???????????List<NameValuePair> objects = new ArrayList<>(); ???????????jsonObject.forEach((k,v)->{ ???????????????BasicNameValuePair basicNameValuePair = new BasicNameValuePair(k, v.toString()); ???????????????objects.add(basicNameValuePair); ???????????}); ???????????StringEntity stringEntity = new UrlEncodedFormEntity(objects,"utf-8"); ???????????httpPost.setEntity(stringEntity); ???????????try(CloseableHttpClient httpclient = HttpClients.createDefault(); ???????????????CloseableHttpResponse execute = httpclient.execute(httpPost);){ ???????????????HttpEntity entity = execute.getEntity(); ???????????????String s = EntityUtils.toString(entity); ???????????????return s; ???????} ???????}catch (Exception e){ ???????????e.printStackTrace(); ???????} ???????return null; ???}}

  

HTTPUTILS

原文地址:https://www.cnblogs.com/yeyongjian/p/10201888.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved