POST请求-查询变量

时间:2018-11-28 18:26:07

标签: rest api post query-variables

我正在尝试通过REST API使用Java的HttpPost POST查询变量中呈现的json输出,但是请求失败...这是我的代码

HttpClient httpClient = HttpClientBuilder.create()
HttpPost request = new HttpPost(myRestAPIEndpoint);
File json = new File(System.getProperty("user.dir") + File.separator + "jsondata.json");
String input = FileUtils.readFileToString(json, "UTF-8");
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("results",input));
request.setEntity(new UrlEncodedFormEntity(nvps,StandardCharsets.UTF_8));
request.setHeader(HttpUtils.CONTENT_TYPE,"application/json");
request.setHeader("Authorization","Basic "+Base64.getEncoder().encodeToString((USERNAME+":"+PASSWORD).getBytes(StandardCharsets.UTF_8)));
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);

您能帮我吗?

谢谢

0 个答案:

没有答案