curl java_如何将curl -X post转换为java
我正在嘗試將curl命令轉換為Java(使用Apache HttpClient 4.x):
export APPLICATION_ID=SOME_ID
export REST_API_KEY=SOME_KEY
curl -i -X POST \n -H "X-Parse-Application-Id: ${APPLICATION_ID}" \n -H "X-Parse-REST-API-Key: ${REST_API_KEY}" \n -H "Content-Type: image/png" \n --data-binary @/Users/thomas/Desktop/greep-small.png \n https://api.parse.com/1/files/greep.png
但出現以下錯誤:{“錯誤”:“未經授權”}.
這是我的Java代碼如下所示:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("localhost", 80, "http");
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("username", "password"));
HttpPost httpPost = new HttpPost("https://api.parse.com/1/files/greep.png");
System.out.println("executing request:
" + httpPost.getRequestLine());
List nameValuePairs = new ArrayList(2);
nameValuePairs.add(new BasicNameValuePair("Example-Application-Id", "SOME_ID"));
nameValuePairs.add(new BasicNameValuePair("Example-REST-API-Key", "SOME_KEY"));
nameValuePairs.add(new BasicNameValuePair("Content-Type", "image/png"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (responseEntity != null) {
System.out.println("Response content length: "
+ responseEntity.getContentLength());
}
System.out.println(EntityUtils.toString(responseEntity));
httpclient.getConnectionManager().shutdown();
如何轉換以-H開頭的卷曲線和以“ –data-binary”開頭的卷曲線? -d的java等效項是什么?
-d '{ "name":"Andrew", "picture": { "name": "greep.png", "__type": "File" } }' \n
任何提示表示贊賞.謝謝
總結
以上是生活随笔為你收集整理的curl java_如何将curl -X post转换为java的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql gone away 测试_p
- 下一篇: java swing 传值_java s