curl https://view-api.box.com/1/documents \
-H "Authorization: Token YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"url": "https://cloud.box.com/shared/static/4qhegqxubg8ox0uj5ys8.pdf"}' \
-X POST
你如何容纳网址?这是我到目前为止所尝试的内容。
final String url = "https://view-api.box.com/1/documents";
@SuppressWarnings("resource")
final HttpClient client = HttpClientBuilder.create().build();
final HttpPost post = new HttpPost(url);
post.setHeader("Authorization", "Token: TOKEN_ID");
post.setHeader("Content-Type", "application/json");
final List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("url", "https://cloud.box.com/shared/static/4qhegqxubg8ox0uj5ys8.pdf"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
final HttpResponse response = client.execute(post);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
final StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
}
答案 0 :(得分:0)
除了实体之外,你有一切正常,你在curl
发送的内容不是html表单的内容,而是json对象。
首先取消这一部分(不要将数据发送为application / x-www-form-urlencoded):
// comment out / delete this from your code:
final List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("url", "https://cloud.box.com/shared/static/4qhegqxubg8ox0uj5ys8.pdf"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
然后以这种方式添加身体:
BasicHttpEntity entity = new BasicHttpEntity();
InputStream body = new ByteArrayInputStream(
"{\"url\": \"https://cloud.box.com/shared/static/4qhegqxubg8ox0uj5ys8.pdf\"}".getBytes());
entity.setContent(body);
post.setEntity(entity);
我假设您的JSON字符串只有0x20和0x7F之间的字符,但如果您使用其他字符(如Ñ),则需要使用编码UTF-8(使用的标准编码)将数据转换为字节数组在JSON数据中)以这种方式:
BasicHttpEntity entity = new BasicHttpEntity();
String myData = "{\"url\": \"https://cloud.box.com/shared/static/4qhegqxubg8ox0uj5ys8.pdf\"}";
ByteArrayOutputStream rawBytes = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(rawBytes,
Charset.forName("UTF-8"));
writer.append(myData);
InputStream body = new ByteArrayInputStream(rawBytes.toByteArray());
entity.setContent(body);
post.setEntity(entity);
答案 1 :(得分:0)
我建议如下 - 虽然我不记得StringEntity是否在HTTPClient下可用
final String url = "https://view-api.box.com/1/documents";
@SuppressWarnings("resource")
final HttpClient client = HttpClientBuilder.create().build();
final HttpPost post = new HttpPost(url);
post.setHeader("Authorization", "Token: TOKEN_ID");
post.setHeader("Content-Type", "application/json");
post.setEntity(new StringEntity("{\"url\": \"https://cloud.box.com/shared/static/4qhegqxubg8ox0uj5ys8.pdf\"}"));
final HttpResponse response = client.execute(post);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
final StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
}