1
我有這樣的要求cURL
:轉換捲曲請求轉換的URLConnection
curl -H 'Accept: application/vnd.twitchtv.v3+json' -H 'Authorization: OAuth <access_token>' \
-X PUT https://api.twitch.tv/kraken/users/<bot_name>/follows/channels/<channel_name>
我需要把它變成一個Java URLConnection
請求。這是我到目前爲止:
String url = "https://api.twitch.tv/kraken/?oauth_token=" + bot.botOAuth.substring("oauth:".length());
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write("https://api.twitch.tv/kraken/users/" + bot.botName + "/follows/channels/" + gamrCorpsTextField.getText());
out.close();
new InputStreamReader(conn.getInputStream());
任何幫助將不勝感激!
謝謝!我添加了'connection.setRequestProperty(「Content-Length」,connection.getContentLength()+「」);'現在它拋出了一個'java.lang.IllegalStateException:Already connected'錯誤。任何想法爲什麼? –
執行任何需要發送HTTP請求的操作後,您不能再設置請求屬性。一個這樣的操作是'getContentLength()',因爲它檢索* response *內容的長度。無論如何您都不需要直接設置* request *的內容長度。只需使用'doOutput(true)',並通過連接的'OutputStream'寫入請求體。 –