document.write("");

Postman Code Java-Unirest 代码的依赖

本来是Postman的Code直接使用的,结果根据这个名字 Unirest,搜出来了很多依赖,使用了排名第一的,

https://search.maven.org/search?q=Unirest

结果发现Postman的Code

Unirest.setTimeouts(0, 0);
在我使用的依赖里根本没有这个的参数
 
再搜一下,就发现了,不是
<dependency>
  <groupId>com.konghq</groupId>
  <artifactId>unirest-java</artifactId>
  <version>4.0.0-RC2</version>
</dependency>

而是

        <dependency>
            <groupId>com.mashape.unirest</groupId>
            <artifactId>unirest-java</artifactId>
            <version>1.4.9</version>
        </dependency>

fo了,

postman代码

Unirest.setTimeouts(0, 0);
HttpResponse<String> response = Unirest.get("urlxxxxx")
  .asString();

结果一运行,居然还报错了,JSONArray 和org.json not found之前的报错

原本项目里有使用

 <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.58</version>
</dependency>

没的办法,

只能exclude

 <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.58</version>
    <exclusions>
        <exclusion>
            <groupId>json</groupId>
            <artifactId>org.json</artifactId>
        </exclusion>
    </exclusions>
</dependency>

然后再单独引入

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20220320</version>
        </dependency>

后来发现,Unriest还有关联的其它的基础依赖

好吧,继续

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpasyncclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
        </dependency>

 

完成!

 请求第三方接口

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

 

    @PostMapping("/Xxxx")
    public ResponseEntity<Object> getRoomInfo2(@Valid @RequestBody DemoData demoDate) throws UnirestException {
//        Unirest.setTimeouts(0, 0); // 模拟浏览器
//        HttpResponse<String> response = Unirest.get("threeUrl")
//                .header("Proxy-Connection", "keep-alive")
//                .header("Cache-Control", "max-age=0")
//                .header("Upgrade-Insecure-Requests", "1")
//                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36")
//                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
//                .header("Accept-Language", "zh-CN,zh;q=0.9")
//                .asString();
        Unirest.setTimeouts(0, 0);
        HttpResponse<String> response = Unirest.get("threeUrl")
                .asString();
        return ResponseEntity.ok(response);
    }

 

 

posted @ 2022-04-15 16:27  人间春风意  阅读(584)  评论(0编辑  收藏  举报