Postman Code Java-Unirest 代码的依赖


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

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

结果发现Postman的Code

Unirest.setTimeouts(0, 0); 在我使用的依赖里根本没有这个的参数   再搜一下,就发现了,不是

  com.konghq
  unirest-java
  4.0.0-RC2

而是

        
            com.mashape.unirest
            unirest-java
            1.4.9
        

fo了,

postman代码

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

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

原本项目里有使用

 
    com.alibaba
    fastjson
    1.2.58

没的办法,

只能exclude

 
    com.alibaba
    fastjson
    1.2.58
    
        
            json
            org.json
        
    

然后再单独引入

        
            org.json
            json
            20220320
        

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

好吧,继续

        
            org.apache.httpcomponents
            httpclient
        
        
            org.apache.httpcomponents
            httpasyncclient
        
        
            org.apache.httpcomponents
            httpmime
        

完成!

 请求第三方接口

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
    @PostMapping("/Xxxx")
    public ResponseEntity getRoomInfo2(@Valid @RequestBody DemoData demoDate) throws UnirestException {
//        Unirest.setTimeouts(0, 0); // 模拟浏览器
//        HttpResponse 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 response = Unirest.get("threeUrl")
                .asString();
        return ResponseEntity.ok(response);
    }