java后端调用第三方系统接口

//获取单点登录token地址
private static final String GET_TOKEN_URL = "getTokenUrl";
/**
 * 模拟单点登录获取token
 * @param username
 * @return
 */
@ResponseBody
@RequestMapping(value = "/ossInterfaceGetToken", method = RequestMethod.POST)
public AjaxResult OSSInterfaceGetToken() {
    String TOKEN = "";
    try {
        URL url = new URL(GET_TOKEN_URL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");

        // 设置请求头部信息
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        // 开启输出流,向接口发送数据
        connection.setDoOutput(true);
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes("key1=value1&key2=value2");
        outputStream.flush();
        outputStream.close();
        // 获取接口返回的数据
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        // 输出接口返回的数据
        logger.info("获取第三方系统对应人员的token:{}",response.toString());
        //System.out.println(response.toString());
        TOKEN = response.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return AjaxResult.success(TOKEN);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java后端调用第三方RESTful接口,你可以使用Java的HTTP客户端库来发送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等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aboboguai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值