对接接口的时候遇到一个问题-postman可以请求成功的接口httpclient请求却报403
今天遇到一个蛋疼的问题,postman请求一个接口,可以通,但是用httpclient发送请求却报403,然后发现是因为在postman请求的时候自动加上了 请求头User-Agent,而httpclient里面没有设置,设置之后请求成功import java.util.ArrayList;import org.apache.commons.httpclient.Header;import org
·
今天遇到一个蛋疼的问题,postman请求一个接口,可以通,但是用httpclient发送请求却报403,然后发现是因为在postman请求的时候自动加上了 请求头User-Agent,而httpclient里面没有设置,设置之后请求成功
import java.util.ArrayList;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import com.harper.util.RequestTLSv12Utils;
/*
* 【Author】 爱吃早餐的程序员
* 【Time】2021年4月15日 下午5:46:25
* 【Function】
*/
public class DapDhlTest {
public static void main(String[] args) {
try {
// 参数
String msgContent = "******参数******";
ArrayList<Header> headers = new ArrayList<Header>();
headers.add(new Header("Content-Type", "application/json"));
headers.add(new Header("User-Agent", "PostmanRuntime/7.26.10")); // 这里的设置是必须的必
headers.add(new Header("USERNAME", "SANDBOX"));
String response = RequestTLSv12Utils.postRequest("https://url/shipment/create", headers, msgContent);
System.err.println("推单接口输出内容:" + response);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
}
}
}
postman请求是自动加的
所以就很蛋疼,如果你不了解http协议,那么面对这些报错的时候是没有办法的。http发请求的时候有的地址会识别User-Agent 如果没检测到就会被拦截,所以报了403错。
更多推荐
人工智能免费视频资源
所有评论(0)