Java HTTPPOST-代码失败,但通过邮递员成功
我正在通过http post发送JSON请求。后端需要我发送两个标题,即-授权和内容类型。然后将原始JSON请求作为主体。我通过Postman工具通过Post方法发送请求,消息成功通过。但是,当我通过代码发送具有相同头的相同请求时,我得到了一个错误的请求-错误 我最小化了要检查的测试范围。 我只通过邮递员发送了两个邮件头,以下是回复: 标题 授权:“持票人”+代币。结构为“载体”关键字+空格+Okta令牌。 内容类型:application/json 响应: 标题 内容类型→应用程序/json;字符集=utf-8 严格的运输安全→最大年龄=31536000 X-Content-Type-Options→诺斯尼夫 请求上下文→appId=cid-v1:dc220dda-e515-4dd4-b324-c078a1b8fac4 日期→2019年5月26日星期日13:23:01 GMT 内容长度→二十九 身体 { “消息”:“无请求正文” } 但是当我通过代码发送同样的东西时,我得到了一个错误的请求。请帮助:(Java HTTPPOST-代码失败,但通过邮递员成功,java,http-post,httpclient,postman,okta-api,Java,Http Post,Httpclient,Postman,Okta Api,我正在通过http post发送JSON请求。后端需要我发送两个标题,即-授权和内容类型。然后将原始JSON请求作为主体。我通过Postman工具通过Post方法发送请求,消息成功通过。但是,当我通过代码发送具有相同头的相同请求时,我得到了一个错误的请求-错误 我最小化了要检查的测试范围。 我只通过邮递员发送了两个邮件头,以下是回复: 标题 授权:“持票人”+代币。结构为“载体”关键字+空格+Okta令牌。 内容类型:application/json 响应: 标题 内容类型→应用程序/json;
HttpClient-httpclient1;
HttpPost-httppost1;
//ArrayList后参数1=null;
httpclient1=HttpClientBuilder.create().build
httppost1=新的HttpPost(Constants.URL);
//httppost1.setEntity(新的UrlEncodedFormEntity(后参数1,“UTF-8”);
//httppost1.setEntity(新的StringEntity(请求,“UTF-8”);
httppost1.setHeader(“授权”,令牌);
setHeader(“内容类型”、“应用程序/json”);
//httppost1.setHeader(“内容长度”,strlength);
//setHeader(“接受”、“应用程序/json”);
//StringEntity=新的StringEntity(请求,ContentType.APPLICATION_JSON);
//httppost1.setEntity(实体);
HttpResponse response1=httpclient1.execute(httppost1);
InputStream is=response1.getEntity().getContent();
HttpEntity entity1=httppost1.getEntity();
Header[]headers=httppost1.getAllHeaders();
System.out.println(标题.长度);
对于(int i=0;i
HttpClient httpclient1;
HttpPost httppost1;
//ArrayList<NameValuePair> postParameters1 = null;
httpclient1 = HttpClientBuilder.create().build
httppost1 = new HttpPost(Constants.URL);
// httppost1.setEntity(new UrlEncodedFormEntity(postParameters1,"UTF-8"));
// httppost1.setEntity(new StringEntity(request,"UTF-8"));
httppost1.setHeader("Authorization", token);
httppost1.setHeader("Content-Type", "application/json");
// httppost1.setHeader("Content-Length", strlength);
// httppost1.setHeader("Accept", "application/json");
// StringEntity entity = new StringEntity(request,ContentType.APPLICATION_JSON);
// httppost1.setEntity(entity);
HttpResponse response1 = httpclient1.execute(httppost1);
InputStream is = response1.getEntity().getContent();
HttpEntity entity1 = httppost1.getEntity();
Header[] headers = httppost1.getAllHeaders();
System.out.println(headers.length);
for(int i=0; i<headers.length;i++)
{
System.out.println(headers[i]);
}
String result = convertInputStreamToStringCommonIO(is);
System.out.println();
System.out.println(result);
int code1 = response1.getStatusLine().getStatusCode();
System.out.println("Code: "+code1+"");
String reason1 = response1.getStatusLine().getReasonPhrase();
System.out.println("Description: "+reason1 +"");