使用httpClient调用接口获取响应数据

转自:https://blog.csdn.net/shuaishuaidewo/article/details/81136088
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

/**
 * 需要注入依赖
 *  <dependency>
*         <groupId>com.squareup.okhttp3</groupId>
 *        <artifactId>okhttp</artifactId>
 *        <version>3.6.0</version>
 *  </dependency>
 */
@Slf4j
public class HttpClientUtils {

    public static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");

    /**
     * get请求获取请求数据
     * @param url
     * @return
     */
    public static String httpGet(String url){
        String getData ;
        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        try {
            Response response = httpClient.newCall(request).execute();
            getData = response.body().string();

        }catch (Exception e){
            log.info("【发送 GET 请求出现异常】!" + e.getMessage());
            return "-1";
        }
        return getData;
    }

    /**
     * post请求获取请求数据
     * @param url
     * @param json
     * json数据的生成方式(可选);
     *      JSONObject json=new JSONObject();
     *      json.put("name","张三");
     *      json.put("sex","男");等
     *      json.toString()
     * @return
     */
    public static String httpPost(String url, String json){
        String postData ;
        OkHttpClient httpClient = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try {
            Response response = httpClient.newCall(request).execute();
            postData = response.body().string();
        }catch (Exception e){
            log.info("【发送 POST 请求出现异常】!" + e.getMessage());
            return "-1";
        }
        return postData;
    }

    public static void main(String[] args) {
        String loadJSON = httpGet("https://vhouyun.com/goods/RankGoodsList?day=yesterday&role=3");
        System.out.println(loadJSON);
        //post方式请求自己测试即可
    }

}

原文地址:https://www.cnblogs.com/yadongliang/p/9665532.html

时间: 2024-08-29 13:32:01

使用httpClient调用接口获取响应数据的相关文章

二级联动:map,for循环一级数据,调用接口获取对象数据依次放到数组里(解决由于后端java是多线程,接收到的数据放入(push)数组中有可能会顺序不对)

解决方法:遍历一级数据时先push一个新的对象,调用接口获取到数据之后splice方法通过index的值判断放入到数组的对应下标下 this.tableData一级数据:  this.relationMaterialNameList二级数据: this.tableData.map((item,index)=>{ this.relationMaterialNameList.push({}) // 编辑页面项目下拉框数据 this.getSpecificationList(item.reimburs

在后台程序中发送http请求并获取响应数据

一,在后台程序中发送http请求获取响应数据 1)以 http://libs.baidu.com/jquery/2.0.0/jquery.min.js 为例 二, 1) String result=""; BufferedReader in = null; URL url = null; try { url = new URL("http://libs.baidu.com/jquery/2.0.0/jquery.min.js"); } catch (Malforme

C#调用接口返回json数据中含有双引号 或其他非法字符的解决办法

这几天,调用别人接口返回json数据含有特殊符号(双引号),当转换成json对象总是报错, json字符格式如下 { "BOXINFO":[ { "ITEM_ID":"100201709153277", "ITEM_NAME":"抗链球菌溶血素"O"(ASO)测定试剂盒(胶乳免)", "QTY":6, "NOTES":"厂牌(河北恒利集团

rest-assured之获取响应数据(Getting Response Data)

我们使用rest-assured可以获得响应内容,比如:我们发起一个get请求 get("/lotto") 并且获得响应内容,我们有多种方式可以实现: 1 // 通过流的方式获得响应内容,在使用完流之后千万不能忘记关闭流 2 InputStream stream = get("/lotto").asInputStream(); 3 //通过字节数组的方式 4 byte[] byteArray = get("/lotto").asByteArray

JAVA调用接口获取数据

package com.zving.zzfw.bl; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import com.zving.appapi.util.HttpClientUtil; import com.zving.framework.json.JSONObject; /** * @author Clover * 登录用户同

ABP 后台调用接口 获取返回的数据

原文:https://www.cnblogs.com/i3yuan/p/10703500.html insert 简单测试: public void test8() { string url = "http://localhost:21021/api/services/app/Role/Create"; var str2 = HttpPost3(url, System.IO.File.ReadAllText("2.txt")); Console.WriteLine(

使用大淘客接口获取产品数据

使用大淘客获取优惠券产品列表 1.申请大淘客账号 2.申请开放平台权限(一般1-3天就可以开通成功) 3.创建应用,创建成功如图所示 这个以9块9包邮为例,获取数据(9.9文档 http://www.dataoke.com/pmc/api-d.html?id=15) 所用到的自定义函数函数如下 /* * 发起GET网络提交 * @params string $url : 网络地址 */ function https_get($url) { $curl = curl_init(); curl_se

通过接口获取后台数据的方法

1.去除数据HTML标签,并控制字数 @Common.ReplaceHtml(@string Contents,string length) 例:@Common.ReplaceHtml(@item.Description,200) 2.格式化文本编辑器内容 @Html.Raw(Common.Format(string Contents)) 例:@Html.Raw(Common.Format(Model.lstNews[0].Contents))

jmeter获取响应数据中参数值的常用方式(JSON提取器 、正则表达式提取器 、XPath提取器 、Bean Shell PostProcessor)

JSON提取器: XPath提取器: 返回数据格式是HTML的时候,选择使用此种方式. 正则表达式提取器: 图中Field to check勾选的是Response Headers,获取的是请求头里的参数值. Bean Shell PostProcessor脚本获取方式: 原文地址:https://www.cnblogs.com/zeqi666/p/10245454.html