HTTPUTILS

摘要:
/版本>fastjson<1.2.47</版本>&书信电报;importorg.apache.http.impl.client.HttpClients;});}HttpGethttpget=newHttpGet(uriBuilder.toString());

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》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Java调用C/C++实现的DLL动态库——JNInginx配置反向代理解决跨域下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SpringBoot之整合Redis

一、SpringBoot整合单机版Redis 1、在pom.xml文件中加入redis的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</art...

1046

Gridland Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4127 Accepted Submission(s): 1900 Problem Description For y...

vue的学习总结---事件处理

v-on的理解 监听DOM元素的事件,并在触发时执行一些js代码 <template> <div> <!-- v-on监听DOM事件,并在触发时做一些js的操作,如下代码可以将js操作直接放在事件中 --> <button v-on:click="num++">点击按钮+1</but...

【WPF】DataGrid动态绑定隐藏列 DataGridTextColumn.Visibility

方法一: 前端 <FrameworkElement x:Name="dummyElement" Visibility="Collapsed"/> <DataGrid> <DataGrid.Columns> <DataGridTextColumn Header="Te...

滚动效果--marquee的使用

1. <marquee></marquee>标签,默认从最右侧往左滚动; 2. marquee 支持的属性    (1)behavior设置滚动方式: <marquee behavior="alternate">我是来回滚动</marquee><marquee behavior="scroll">...

jQuery Easing 动画效果扩展--使用Easing插件,让你的动画更具美感。

jQuery  Easing 是一款比较老的jQuery插件,在很多网站都有应用,尤其是在一些页面滚动、幻灯片切换等场景应用比较多。它非常小巧,且有多种动画方案供选择,使用简单,而且免费。 引入Easing js文件 该插件基于jQuery,所以需要同时引入jQuery库文件和Easing js文件。 <script type="text/javas...