java-Unirest 的post、get请求

get请求,返回值是[{},{}]复杂格式

  HttpResponse<JsonNode> jsonResponse = Unirest.get("http://xxxxx")
          .header("Accept", "application/json, text/javascript, */*; q=0.01")
          .header("Accept-Encoding", "gzip, deflate")
          .header("Accept-Language", "zh-CN,zh;q=0.9")
          .header("appid", appid)
          .header("Connection", "keep-alive")
          .header("Content-Type", "application/json")
          .header("Cookie", cookie)
          .header("Referer", "http://xxxxxx")
          .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36")
          .header("uuid", uuid)
          .header("Host", "xxxxx")
          .header("X-Requested-With", "XMLHttpRequest")
          .asJson();

get请求,反馈值是{}格式

HttpResponse<String> jsonResponse = Unirest.get("http://xxxx")
        .header("Accept", "application/json, text/javascript, */*; q=0.01")
        .header("Accept-Encoding", "gzip, deflate")
        .header("Accept-Language", "zh-CN,zh;q=0.9")
        .header("Connection", "keep-alive")
        .header("Content-Type", "application/json")
        .header("Cookie", result)
        .header("Referer", "http://xxxxx")
        .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36")
        .header("Host", "xxxxxx")
        .header("X-Requested-With", "XMLHttpRequest")
        .asString();

post请求方式1,反馈的值是HttpResponse [{},{}]

        HttpRequestWithBody conn = Unirest.post("https://xxxxx");
        conn.header("Accept","application/json, text/javascript, */*; q=0.01");
        conn.header("Accept-Encoding","gzip, deflate, br");
        conn.header("Accept-Language","zh-CN,zh;q=0.9");
        conn.header("Connection","keep-alive");
//        conn.header("Content-Length","164");
        conn.header("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        conn.header("Cookie","xxxxxzM2EtMjAyZThhZDg4OTJk");
        conn.header("Host","xxx);
        conn.header("Origin","https://sxxxx");
        conn.header("Referer","https://sxxxxx");
        conn.header("sec-ch-ua","\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"98\", \"Google Chrome\";v=\"98\"");
        conn.header("sec-ch-ua-mobile","?0");
        conn.header("sec-ch-ua-platform","\"Windows\"");
        conn.header("Sec-Fetch-Dest","empty");
        conn.header("Sec-Fetch-Mode","cores");
        conn.header("Sec-Fetch-Site","same-origin");
        conn.header("sw8","ccccc");
        conn.header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36");
        conn.header("X-Requested-With","XMLHttpRequest");
        conn.header("x-tracerid","e76fa74a4a64454a8fde5c220f85896c");
	
	//这里是重点,使用field不行	
	conn.body("pageIndex=0&startDate=&endDate=&orderBy=publishTime+desc&resourceType=&scene=-1&level=-1&title=&pageType=&itemsPerPage=20&pbiIds=PBI2-21110924%2C&level1=&ftreePath=");
//        conn.field("pageIndex","0");
//        conn.field("startDate","");
//        conn.field("endDate","");
//        conn.field("orderBy","publishTime desc");
//        conn.field("resourceType","");
//        conn.field("scene","-1");
//        conn.field("level","-1");
//        conn.field("title","");
//        conn.field("pageType","");
//        conn.field("itemsPerPage","20");
//        conn.field("pbiIds","PBI2-21110924,");
//        conn.field("level1","");
//        conn.field("ftreePath","");

        try {
            HttpResponse<JsonNode> result = conn.asJson();

            System.out.println(result.getHeaders());

            System.out.println(result.getBody().toString());
            JsonNode resBodyRelate = result.getBody();

            JSONObject resObjectRelate = resBodyRelate.getObject();
        } catch (UnirestException e) {
            e.printStackTrace();
        }

post请求方式2,反馈的值是HttpResponse,{}

      CookieHandler cookieManager = CookieHandler.getDefault();
        Map<String, List<String>> result = cookieManager.get(new URI("http://xxxxxxxxx"), new HashMap<>());
        java.util.List cookies = result.get("Cookie");
        String cookie = (String) cookies.get(0);
        System.out.println(cookie);

		//构造参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("dd", "xxx");
        paramMap.put("ee", xx);

        HttpRequestWithBody conn = Unirest.post("33sdfsdfsdfsdfsdf");
        conn.header("Accept", "application/json, text/javascript, */*; q=0.01");
        conn.header("Accept-Encoding", "gzip, deflate");
        conn.header("Accept-Language", "zh-CN,zh;q=0.9");
        conn.header("Connection", "keep-alive");
        conn.header("Content-Type", "application/json");
        conn.header("Cookie",cookie);
        conn.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36");
        conn.body(JSON.toJSONString(paramMap));

        HttpResponse<String> result1 = conn.asString();
        System.out.println(result1.getBody().toString());
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Vue.js 是一个 JavaScript 框架,使用它可以方便地进行前端开发。vue-resource 是 Vue.js 的一个插件,可以用来发送 HTTP 请求。 关于请求类型,vue-resource 支持 POST 和 GET 两种请求方式。使用方式如下: - GET 请求: ```javascript this.$http.get('your_api').then(response => { // success callback }, response => { // error callback }); ``` - POST 请求: ```javascript this.$http.post('your_api', { // request body }).then(response => { // success callback }, response => { // error callback }); ``` 关于请求格式,vue-resource 支持 form 和 json 两种格式。默认情况下,vue-resource 会将请求体序列化为 application/x-www-form-urlencoded 格式,如果需要发送 json 格式的请求,需要手动设置请求头: ```javascript this.$http.post('your_api', { // request body }, { headers: { 'Content-Type': 'application/json' } }).then(response => { // success callback }, response => { // error callback }); ``` ### 回答2: vue-resource 是 Vue.js 的一个插件,用于发送 HTTP 请求。在 Vue.js 中发送 POST 和 GET 请求可以使用 vue-resource 的形式处理。下面是一个例子,展示如何用 vue-resource 进行 POST 和 GET 请求,发送 Form 表单数据和 JSON 数据。 发送 POST 请求: 1. 使用 vue-resource 的 $http.post() 方法发送 POST 请求。 2. 将要发送的数据作为参数传递给 $http.post() 方法。 3. 如果要发送 Form 表单数据,可以将数据作为对象传递给 $http.post() 方法。 4. 如果要发送 JSON 数据,需要在请求头中设置 Content-Type 为 application/json,并将 JSON 数据作为字符串传递给 $http.post() 方法。 下面是使用 vue-resource 发送 POST 请求的示例: ``` js // 发送 Form 表单数据 this.$http.post('api/url', {param1: value1, param2: value2}).then((response) => { // 处理成功的回调 }).catch((error) => { // 处理失败的回调 }); // 发送 JSON 数据 let jsonData = JSON.stringify({param1: value1, param2: value2}); this.$http.post('api/url', jsonData, {headers: {'Content-Type': 'application/json'}}).then((response) => { // 处理成功的回调 }).catch((error) => { // 处理失败的回调 }); ``` 发送 GET 请求: 1. 使用 vue-resource 的 $http.get() 方法发送 GET 请求。 2. 将要发送的数据作为参数传递给 $http.get() 方法。 3. 参数可以直接拼接在 URL 中,也可以作为一个对象传递给 $http.get() 方法。 下面是使用 vue-resource 发送 GET 请求的示例: ``` js // 直接拼接参数在 URL 中 this.$http.get('api/url?param1=value1&param2=value2').then((response) => { // 处理成功的回调 }).catch((error) => { // 处理失败的回调 }); // 作为参数对象传递 this.$http.get('api/url', {params: {param1: value1, param2: value2}}).then((response) => { // 处理成功的回调 }).catch((error) => { // 处理失败的回调 }); ``` 以上就是使用 vue-resource 发送 POST 和 GET 请求时,发送 Form 表单数据和 JSON 数据的方法。 ### 回答3: Vue-resource是Vue.js官方推荐的一个用于处理网络请求的插件。使用Vue-resource可以很方便地进行postget请求,并且可以以form和json的形式进行数据传输。 首先,我们来讲解如何进行post请求。在Vue组件中,可以通过Vue-resource的$http对象来发送post请求。要发送一个form形式的post请求,可以使用`this.$http.post(url, formData)`方法,并将form数据作为第二个参数传递进去。 例如,假设我们要向服务器端发送一个form形式的post请求,可以通过以下代码实现: ``` this.$http.post('/api/users', this.formData).then(response => { console.log(response.body); }, error => { console.error(error); }); ``` 其中,`/api/users`是请求的url,`this.formData`是包含form数据的对象。通过`then`方法可以处理请求成功和失败的情况,并且可以从`response`对象中获取到服务器返回的数据。 下面我们来看一下如何进行get请求。同样地,在Vue组件中,可以通过Vue-resource的$http对象来发送get请求。要发送一个json形式的get请求,可以使用`this.$http.get(url, {params: jsonData})`方法,并将json数据作为`params`属性传递进去。 举一个例子,如果我们需要向服务器端发送一个json形式的get请求,可以按照以下方式实现: ``` this.$http.get('/api/users', {params: {name: 'Alice', age: 25}}).then(response => { console.log(response.body); }, error => { console.error(error); }); ``` 在这个例子中,`/api/users`是请求的url,`{name: 'Alice', age: 25}`是需要传递给服务器的json数据。同样地,在`then`方法中可以处理请求成功和失败的情况,并且从`response`对象中获取到服务器返回的数据。 总之,使用Vue-resource可以轻松地发送postget请求,并且可以自由选择传递form或json形式的数据。以上就是关于如何使用Vue-resource进行postget请求的简要说明。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值