获取header中的token串&&http的patch方式发送请求
Posted 健康平安的活着
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取header中的token串&&http的patch方式发送请求相关的知识,希望对你有一定的参考价值。
一 java后端获取token串
使用java代码获取前端发来的请求header中的token串,
1.1 模拟登陆,获取token
地址:http://10.130.7.118:81/cas/jwt/login
方式:post
header:content-type:application/x-www-form-urlencoded
请求参数:
1.2 请求上下线的接口地址
请求地址中:header设置token,进行请求
1.3 请求自定义的程序,获取header的token
后端代码:
打印的日志:
二 java后端模拟http的pacth的方式进行请求
方式一: 使用URL类的方式请求,使用patch,返回包405错误,不支持patch方式吧
方式二:使用httpclient方式: 能够获取到header的信息,状态码,获取返回的数据,通过httpEntity也无法获取数据,但是通过 result=EntityUtils.toString(entity,"UTF-8");解析后就能返回数据;但是感觉查询耗时比较长。
postman返回结果
httpclient的依赖
完整代码:
package com.ljf.spring.boot.demo.controller;
import com.ljf.spring.boot.demo.common.ResponseResult;
import com.ljf.spring.boot.demo.model.ProductTotalVo;
import com.ljf.spring.boot.demo.model.Test;
import org.apache.commons.codec.CharEncoding;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @ClassName: GetTokenController
* @Description: TODO
* @Author: liujianfu
* @Date: 2021/12/05 17:26:35
* @Version: V1.0
**/
@RestController
@RequestMapping("/dispatch-task")
public class GetTokenController
// String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ0Z3QiOiJUR1QtMTkzLWVZYk55RFVzYXJLTzZrWG5oVHBmLUEzeWpGT3pnNGktNHpiZ2V3Nm9sbXFwUkI2bzNCY3lvZVJJZkxtbmsxamRzZGNjYXMiLCJkdXJhdGlvbiI6IjcyMDAiLCJhdWQiOiJhbGwiLCJzdWIiOiJib25jIiwiaXNzIjoiY2xvdWRpaXAiLCJleHAiOjE2Mzg3MDMxMDUsInVzZXJuYW1lIjoiYWRtaW4ifQ.oX_oXsGnG2_PvUqDiWePsx-h_hdi7lW83upQ-Tz4goN0GNDFwM_L0jgs3v0Ty9KglK4RJtVdd1SoBVvWArk_nlyiBcJrNu3YO10hvKl0L_DUNlD4xYJdybb-GZ7_hovWtv85daJwoBysMpe71vFbLj2tmvqZMamI9mh7yl6UWoFEqhAPr76RCCrD1moIpxD5XUv8PSAJ26EfelT3PN7vWl_kkH9mnsiepD79_xvLXwCxDjxzNoUy8LzxzSeU75L84l9q5E8m4tfa12UvhoXZxZ1eJ7FXiaDe2sTh9up4J0cXzze0qspln7Gar5rUigAPyiuE_30nFwD5siNz6X2Vgw";
@RequestMapping("/info")
public ResponseResult productInfoTotal(HttpServletRequest request) throws Exception
System.out.println("======进入controller,下发任务");
String token=request.getHeader("Authorization");
System.out.println("获取token为:"+token);
String httpURL="http://10.130.7.118:81/cloudiip-access/prod/event/alarm/NbQugyvX_1635410805796_2?disableFlag=0";
//String str= sendHttpRequest(httpURL,token);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPatch patch= new HttpPatch(httpURL);
patch.setHeader("Authorization",token);
HttpResponse response = httpClient.execute(patch);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
System.out.println("entity:"+entity);
String result="";
// 解析数据封装HttpResult
if (response.getEntity() != null)
result=EntityUtils.toString(entity,"UTF-8");
//httpResult = new HttpResult(response.getStatusLine().getStatusCode(),EntityUtils.toString(response.getEntity(),"UTF-8"));
// httpResult.setCode(response.getStatusLine().getStatusCode());
//httpResult.setBody(EntityUtils.toString(response.getEntity(),"UTF-8"));
return ResponseResult.ok(statusCode+" 获取成功:"+result);
public String sendHttpRequest(String httpURL,String token) throws Exception
//1. 定义需要访问的地址 "https://way.jd.com/he/freeweather"
URL url = new URL(httpURL);
//2. 开启连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//3. 设置请求的方式
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
connection.setRequestProperty("Authorization",token);
//4. 输出参数
connection.setDoOutput(true);
//7. 发起请求
connection.connect();
//8. 接收对方响应的信息,可以使用Spring的 StreamUtils 工具类
String response = StreamUtils.copyToString(connection.getInputStream(), Charset.forName("UTF-8"));
System.out.println(connection.getResponseCode());
System.out.println(connection.getResponseMessage());
return response;
以上是关于获取header中的token串&&http的patch方式发送请求的主要内容,如果未能解决你的问题,请参考以下文章
LARAVEL & VUE:如何通过 API 请求获取登录用户的 API_TOKEN?
HttpRunner中获取Response Header中的参数
HttpRunner中获取Response Header中的参数
Backbone & Slim PHP - Access-Control-Allow-Headers - 可以获取信息,不能发布吗?