• HTTPUTILS


    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;
        }
    }
    

      

  • 相关阅读:
    postman环境和全局变量设置语句
    2016 GitHub章鱼猫观察报告之开源统计
    Multiload-ng
    忠告初学者学习Linux系统的8点建议
    真有用?Snap和Flatpak 通吃所有发行版的打包方式。
    教你如何在Kali Linux 环境下设置蜜罐?
    下一代GNU/Linux显示服务Wayland 1.12正式发布
    为 Github 创造 Integration
    简单易懂的crontab设置工具集
    爆料喽!!!开源日志库Logger的剖析分析
  • 原文地址:https://www.cnblogs.com/yeyongjian/p/10201888.html
Copyright © 2020-2023  润新知