【JAVA】使用Unirest.post模拟post接口,返回状态码403,报错Invalid CORS request
背景
在使用unirest发起post请求时,按照以往的操作步骤:拷贝请求head,然后构造body,结果运行程序时报错Invalid CORS request,百思不得其解。
核心HEADER代码
conn.header("host", "xxxx");
conn.header("origin", "https://x1");
conn.header("referer", "https//x2");
conn.header("sec-ch-ua", "\"Chromium\";v=\"134\", \"NotA-Brand\";v=\"24\", \"Google Chrome\";v=\"134\"");
conn.header("sec-ch-ua-mobile", "?0");
conn.header("sec-ch-ua-platform", "\"Windows\"");
conn.header("sec-fetch-dest", "empty");
conn.header("sec-fetch-mode", "cors");
conn.header("sec-fetch-site", "same-origin");
解决办法
conn.header("origin", "https://x1");
里面的网页不是f12里面的request header中的origin,是填写的浏览器上的url。