java 发送http请求携带body参数以及head携带token

	在日常开发过程中,对接第三方难免需要通过javaAPI发送各种请求去请求数据,这里给一个发送http请求携带body参数、head参数。
 /**
     * post请求携带body参数
     * @param serverURL
     * @param params  body参数 application/json
     * @return
     */
    public static String sendPostBody(String serverURL, String params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        InputStream is = null;
        OutputStreamWriter writer = null;
        try{
            StringBuffer sbf = new StringBuffer();
            String strRead = null;
            URL url = new URL(serverURL);
            connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("POST");//请求post方式
            connection.setDoInput(true);
            connection.setDoOutput(true);
            //header内的的参数在这里set
            connection.setRequestProperty("geoToken", "token");
            connection.setRequestProperty("Content-Type", "application/json;charset=\"UTF-8\"");
            connection.connect();
            writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
            //body参数放这里
            writer.write(params);
            writer.flush();
            is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            is.close();
            writer.close();
            connection.disconnect();
            String results = sbf.toString();
            System.out.println("str_base>>>:"+results);
            return results;
        }catch (IOException e){
            e.printStackTrace();
            return "";
        }finally {
            try {
                if(connection != null){
                    connection.disconnect();
                }
                if(reader != null){
                    reader.close();
                }
                if(is != null){
                    is.close();
                }
                if(writer != null){
                    writer.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java发送 HTTP 请求携带 token 可以通过添加 HTTP 头部信息来实现。具体的做法是: 1. 创建一个 `HttpURLConnection` 对象,并设置请求的 URL。 ```java URL url = new URL("http://example.com/api"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); ``` 2. 添加 HTTP 头部信息,其中 Authorization 头部用于携带 token。 ```java String token = "your_token_here"; connection.setRequestProperty("Authorization", "Bearer " + token); ``` 3. 发送请求并获取响应。 ```java connection.setRequestMethod("GET"); // 设置请求方法,例如 GET、POST 等 int responseCode = connection.getResponseCode(); // 获取响应状态码 String responseBody = ""; // 响应正文 try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String line; while ((line = in.readLine()) != null) { responseBody += line; } } ``` 完整的代码示例: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpWithToken { public static void main(String[] args) throws Exception { URL url = new URL("http://example.com/api"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); String token = "your_token_here"; connection.setRequestProperty("Authorization", "Bearer " + token); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); String responseBody = ""; try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String line; while ((line = in.readLine()) != null) { responseBody += line; } } System.out.println("Response code: " + responseCode); System.out.println("Response body: " + responseBody); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值