Java使用UniRest发送Http/Https请求

6 篇文章 0 订阅
3 篇文章 0 订阅

文末附有包装类,复制即用

使用UniRest发送POST、GET、PUT、DELETE请求非常方便,还可以用各种格式的请求体发送请求,比如params格式、body的raw、form-data格式等等。

使用方法如下

首先引入maven包,作为依赖包。

		<dependency>
            <groupId>com.konghq</groupId>
            <artifactId>unirest-java</artifactId>
            <version>3.0.00</version>
        </dependency>
		
		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>

Http请求

1. 发送POST请求

raw格式(json)

	public JSONObject doPost(String url, String json) {
        HttpResponse jsonResponse = null;
        try {
            JsonNode jsonNode = new JsonNode(json);
            jsonResponse = Unirest.post(url)
                    .body(jsonNode)
                    .header("content-type", "application/json")
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

raw格式(string)

	public JSONObject doPostBytxt(String url,String txt) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.post(url)
                    .body(txt)
                    .header("content-type", "application/json")
                    .asString();
        } catch (UnirestException e) {
            e.printStackTrace();
        }

        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

params格式(map 上传文件可以用这个)

    public JSONObject doPostByParams(String url, Map<String ,Object> map) {
        HttpResponse jsonResponse = null;

        try {
            jsonResponse = Unirest.post(url)
                    .fields(map)
                    .asString();
        } catch (UnirestException e) {
            e.printStackTrace();
        }

        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

2. 发送GET请求

无参

	public JSONObject doGet(String url) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.get(url)
                    .header("content-type", "application/json")
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

params格式参数(map)

    public JSONObject doGet(String url,Map<String,Object> map) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.get(url)
                    .header("content-type", "application/json")
                    .queryString(map)
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

3. PUT请求

无参数(或者参数拼接在url后面的)

    public JSONObject doPut(String url) {
        kong.unirest.HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.put(url)
                    .header("content-type", "application/json")
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

raw格式(json)

    public JSONObject doPut(String url, String json) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.put(url)
                    .body(json)
                    .header("content-type", "application/json")
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

4.DELETE

无参数

    public JSONObject doDelete(String url) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.delete(url)
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

Https请求

https请求只需要在这个类里添加一个静态代码块即可

	static {
        Unirest.config().verifySsl(false);
    }

一些不常用的我就没写在这里,谢谢,希望对你有所帮助
在这里插入图片描述

附上整体代码,复制粘贴即用

package com.oushu.utils.util;

import com.alibaba.fastjson.JSONObject;
import kong.unirest.*;

import java.util.Map;

public class HttpUtil {
    static {
        Unirest.config().enableCookieManagement(true);
        Unirest.config().verifySsl(false);
        Unirest.config().connectTimeout(30000);
        Unirest.config().automaticRetries(true);
    }

    static HttpUtil instance;

    public static HttpUtil getInstance() {
        if (instance == null) {
            instance = new HttpUtil();
        }
        return instance;
    }

    public JSONObject doGet(String url) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.get(url)
                    .header("content-type", "application/json")
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

    public JSONObject doGet(String url, Map<String, Object> map) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.get(url)
                    .header("content-type", "application/json")
                    .queryString(map)
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

    public JSONObject doPut(String url) {
        kong.unirest.HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.put(url)
                    .header("content-type", "application/json")
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

    public JSONObject doPut(String url, String json) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.put(url)
                    .body(json)
                    .header("content-type", "application/json")
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

    public JSONObject doPutByForm(String url, Map<String, Object> map) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.put(url)
                    .fields(map)
                    .asString();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

    public JSONObject doDelete(String url) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.delete(url)
                    .header("content-type", "application/json")
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

    public JSONObject doDelete(String url, String json) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.delete(url)
                    .body(json)
                    .header("content-type", "application/json")
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }


    public JSONObject doPost(String url, String json) {
        HttpResponse jsonResponse = null;
        try {
            JsonNode jsonNode = new JsonNode(json);
            while (jsonResponse == null) {
                jsonResponse = Unirest.post(url)
                        .body(jsonNode)
                        .header("content-type", "application/json")
                        .asJson();
            }
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

    public void doPostParallel(String url, String json) {
        JsonNode jsonNode = new JsonNode(json);
        try {
            Unirest.post(url)
                    .body(jsonNode)
                    .header("content-type", "application/json")
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
    }

    public JSONObject doPostBytxt(String url, String txt) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.post(url)
                    .body(txt)
                    .header("content-type", "application/json")
                    .asString();
        } catch (UnirestException e) {
            e.printStackTrace();
        }

        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

    public JSONObject doPostByForm(String url, Map<String, Object> map) {
        HttpResponse jsonResponse = null;
        try {
            jsonResponse = Unirest.post(url)
                    .fields(map)
                    .asString();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonResponse.getBody().toString());
    }

}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值