java 调用第三方url接口

java 调用第三方url接口

1、GET请求

/**
     * address
     * param 参数
     * get请求
     */
    public static String get(String address,String param ){
        HttpURLConnection connection = getConnection(address);
        try {
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("param ", param );
        } catch (ProtocolException e) {
            e.printStackTrace();
        }
        return access(connection);
    }

2、POST请求


```bash
/**
     * post请求
     * address 地址
     * param 参数
     * token
     */
    public static String post(String address,String param,String token){
        HttpURLConnection connection = getConnection(address);
        try {
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);

            // 设置维持长连接
            connection.setRequestProperty("Connection", "Keep-Alive");

            // COOKIE
            connection.setRequestProperty("Cookie", "aaa=bbb;ccc=ddd;fid=789");
            if(StringUtils.isNotEmpty(token)){
                connection.setRequestProperty("token", token);
            }
            // USER-AGENT
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36");
            connection.setRequestProperty("Content-Type","application/json;charset=utf-8");
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(param.getBytes(StandardCharsets.UTF_8));

            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return access(connection);
    }
/**
     * 访问网络
     */
    private static String access(HttpURLConnection connection){
        StringBuilder result = new StringBuilder();
        try {
            connection.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            reader.close();
            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }
调用

```java
public static void main(String[] args) {
        String appid = "XXX";
        String key = "XXX";
        String url = "XXX";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appid", appid);
        jsonObject.put("key", key);
        String data = NetUtil.post(url,jsonObject.toJSONString(),"");
        JSONObject obj  = JSONObject.parseObject(data);
        String token ="";
        if(obj!=null){
            JSONObject json = obj.getJSONObject("data");
            token = json.getString("token");
        }
        System.out.println(token);
    }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值