JAVA后端——调用第三方URL接口

前言

在项目里有时候需要在后端引用别人的url,java用来调用的方法也有好几种,比如HttpURLConnection,还有apache的HttpClient,简单封装了一下HttpClient的get和post的方法。一般情况下应该是够用了

内容

添加依赖

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.10</version>
</dependency>
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.62</version>
</dependency>

get请求

get方式:请求报文没有请求体,提交参数时,参数在url地址后拼接url?k=v&k2=v2

/**
     * @Description: get方式
     * @author wch001
     * @date 2022/5/6 9:50
     * @param url 地址
     * @param param 参数
     * @param token url携带的headers(非必须)
     * @return java.lang.Object
     */
public static Object requestGetUrl(String url, Map<String, String> param,String token) throws Exception{

        InputStream is = null;
        String body = null;
        StringBuilder  res=new StringBuilder();
        // 设置完整的url
        URIBuilder uriBuilder = null;
        uriBuilder = new URIBuilder(url);
        //添加参数
        for (Map.Entry<String, String> entry : param.entrySet()) {
            uriBuilder.setParameter(entry.getKey(),entry.getValue());
        }
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        httpGet.addHeader("Content-Type", "application/json");
        if(token!=null){
            httpGet.addHeader("token",token);
        }


        RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).build();

        httpGet.setConfig(config);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if(entity != null){
            is = entity.getContent();
            //转换为字节输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
            while((body=br.readLine()) != null){
                res.append(body);
            }
        }


        Object jsonMap = JSON.parse(res.toString());


        return jsonMap;
    }

post请求

post方式:请求报文有请求体, post的请求参数通过请求体提交,如果参数在请求体中是json字符串,后端controller方法需要使用 @RequestBody接受入参

/**
     * @Description: post方式获取访问url接口
     * @author wch001
     * @date 2022/4/29 9:40
     * @param url : 地址
     * @param param : 参数
     * @param token : url携带的headers(非必须)
     * @return java.lang.Object
     */
    public static Object requestPostUrl(String url, Map<String, Object> param,String token) throws Exception{

        InputStream is = null;
        String body = null;
        StringBuilder   res=new StringBuilder();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json");
        if(token!=null){
            httpPost.addHeader("token",token);
        }

        // 设置请求的参数
        JSONObject jsonParam = new JSONObject();

        param.forEach((k,v)-> jsonParam.put(k,v));

        StringEntity stringEntity = new StringEntity(jsonParam.toString(), "utf-8");

        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");
        httpPost.setEntity(stringEntity);

        RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).build();

        httpPost.setConfig(config);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        if(entity != null){
            is = entity.getContent();
            //转换为字节输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
            while((body=br.readLine()) != null){
                res.append(body);
            }
        }


        Object jsonMap = JSON.parse(res.toString());


        return jsonMap;
    }

http协议问题

404: 路径找不到
405: 请求方式不支持
403: 权限
200: 成功
500: 服务器错误
503: 网关找不到对应的微服务处理请求
400: 请求参数错误
302:  重定向

总结

调用完,直接 JSON.parseObject(res.toString()),转成json,就比较好处理了。简单记录一下,以后可能还用的到。

  • 7
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java后端调用第三方RESTful接口,你可以使用JavaHTTP客户端库来发送HTTP请求。以下是一个简单的示例代码,演示如何使用Apache HttpClient库发送GET请求: ```java import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import java.io.IOException; public class RestApiClient { public static void main(String[] args) { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); String url = "https://api.example.com/endpoint"; // 替换为目标接口URL HttpGet httpGet = new HttpGet(url); try { HttpResponse response = httpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println("Status code: " + statusCode); System.out.println("Response body: " + responseBody); } catch (IOException e) { e.printStackTrace(); } finally { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 上述代码中,我们使用了Apache HttpClient库来创建一个HTTP客户端,并构造了一个GET请求。然后,我们使用这个客户端发送请求,并获取响应的状态码和响应体。最后,记得关闭HTTP客户端。 这只是一个简单的示例,你可以根据实际需求进行定制化开发,例如添加请求头、传递请求参数等。请注意,这只是其中一种实现方式,还有其他的HTTP客户端库可供选择,如OkHttp、Spring RestTemplate等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值