HttpClient如何使用Java将请求标头参数JSON作为变量传递

时间:2018-09-23 05:54:45

标签: java http post httpclient http-request-parameters

HttpClient httpclient = HttpClientBuilder.create().build(); 
String authHeader = "Basic dGVzdDpwYXNzd29yZA=="; 
String tsr_id = "NL_EAMP_NL1392"; 
String SIDate = "{\"configuration\":{\"tsrId\":" + tsr_id + ",\"relatedRef\":null,\"validTsrId\":false,\"startDate\":\"2013-12-19\",\"stopDate\":\"2045-01-01\",\"calculationConfigurations\":[{\"id\":null,\"tsrId\":" + tsr_id + ",\"startDate\":\"2008-08-01\",\"stopDate\":\"3000-01-01\",\"scheduleOverrides\":{\"1\":false,\"2\":false,\"7\":false,\"8\":false,\"9\":false,\"10D\":true,\"10E\":true,\"10F\":true,\"23D\":false,\"23E\":false,\"23F\":false,\"26\":false,\"33\":false,\"41\":false,\"42A\":false,\"42B\":false,\"45\":false}}],\"priceZoneConfigurations\":[],\"attributeConfigurations\":[],\"flagConfigurations\":[]},\"note\":\"\"}"; 
HttpPost request = new HttpPost("http:url/configurations"); 
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader); request.setHeader("Content-type", "application/json"); 
StringEntity entity = new StringEntity(SIDate); 
request.setEntity(entity);
HttpResponse resp = httpclient.execute(request); 
int status; 
status = resp.getStatusLine().getStatusCode(); 
assert status == 200;

这是我的Java程序,用于设置SIDate(有效负载/请求参数)中提到的标志。当我对NL_EAMP_NL1392(tsr_id)进行硬编码时,发布请求工作正常。但我声明并将其作为变量传递,却遇到Bad Request 400错误。如何传递tsr_id而不是NL_EAMP_NL1392来请求参数。 TIA

0 个答案:

没有答案