我試圖發送GET
請求到Imgur API上傳圖像。使用Java的Imgur API請求返回400狀態
當我使用以下代碼時,我收到來自Imgur服務器的400
狀態響應 - 根據,這意味着我缺少或具有不正確的參數。
我知道的參數是正確的,因爲我已經直接在瀏覽器URL(其成功上傳的圖像),對它們進行測試 - 所以我不能在代碼中正確地添加參數:
private void addImage(){
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode("http://www.lefthandedtoons.com/toons/justin_pooling.gif", "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode("myPublicConsumerKey", "UTF-8");
// Send data
java.net.URL url = new java.net.URL("http://api.imgur.com/2/upload.json");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
Logger.info(line);
}
wr.close();
rd.close();
}
此代碼基於API示例provided by Imgur。
任何人都可以告訴我我做錯了什麼,我可以如何解決問題?
謝謝。
你有沒有試過包括可選的'type'參數值爲'url'? –
另外,僅僅爲了它(因爲一切看起來都正確),嘗試不帶.json後綴,並查看它是否適用於XML響應。在wr.flush做什麼之後, –
會添加conn.connect()嗎? – MeBigFatGuy