[英]Converting a CURL Request to a HTTP Request Java
我有以下CURL請求,誰能請我確認subesquest HTTP請求是什么?
curl -H "Authorization: Bearer <access token>" --data-urlencode "model=" --data-urlencode "imageurl.img.com" apiurl.api.com
我試過的
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Bearer " + token);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
String payload = "model=\"\"&url=" + url;
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();
問題是我不斷收到403 HTTP錯誤代碼。 它可以與CURL一起使用,因此我可能無法正確轉換它。
我該怎么做才能進行轉換?
@編輯
API: http: //wiki.vg/Mojang_API#Change_Skin Mojang的示例:
curl -H "Authorization: Bearer <access token>" --data-urlencode "model=" --data-urlencode "url=http://assets.mojang.com/SkinTemplates/steve.png" https://api.mojang.com/user/profile/<uuid>/skin
問題中的curl
調用導致POST正文為model=&imageurl.img.com
:
$ curl --trace-ascii - -H "Authorization: Bearer <access token>" \
--data-urlencode "model=" --data-urlencode "imageurl.img.com" https://example.com
=> Send header, 183 bytes (0xb7)
0000: POST / HTTP/1.1
0011: Host: example.com
0024: User-Agent: curl/7.51.0
003d: Accept: */*
004a: Authorization: Bearer <access token>
0070: Content-Length: 23
0084: Content-Type: application/x-www-form-urlencoded
00b5:
=> Send data, 23 bytes (0x17)
0000: model=&imageurl.img.com
== Info: upload completely sent off: 23 out of 23 bytes
具體來說, curl
調用不包含url=
參數。
相反,Java似乎導致POST主體為model=""&url=imageurl.img.com
假設其他代碼(問題中未顯示)將url
變量設置為imageurl.img.com
。
我猜可能是因為沒有代碼將url
變量設置為imageurl.img.com
。 僅憑問題片段無法分辨。
curl
調用還會發送標頭Accept: */*
而Java代碼則發送標頭Accept: application/json
。 我不認為這會導致403,但誰知道。
另外, curl
調用還會發送一個User-Agent: curl/7.51.0
標頭,但是尚不清楚Java代碼是否導致發送User-Agent
標頭。 我以為是,但又一次,誰知道。
您正在設置屬性,但應指定標題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.