2014-07-06 26 views
1

我試圖從Java這種捲曲的要求執行:的Java HttpsURLConnection的

curl -H 'Accept: application/vnd.twitchtv.v2+json' \ 
-d "channel[status]=testing+some+stuff" \ 
-X PUT https://api.twitch.tv/kraken/channels/testacc222?oauth_token=6e7b9cyfi8zk1gr8g06eecebnitlcvb 

我的解決辦法是這樣的:

public static void main(String args[]) throws IOException { 

    String uri = "https://api.twitch.tv/kraken/channels/testacc222?oauth_token=6e7b9cyfi8zk1gr8g06eecebnitlcvb"; 
    URL url = new URL(uri); 

    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
    conn.setRequestMethod("PUT"); 
    conn.setDoOutput(true); 
    conn.setRequestProperty("Accept", "application/vnd.twitchtv.v2+json"); 

    String data = "channel[status]=testing"; 
    OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); 
    out.write(data); 
    out.flush(); 
    for (Entry<String, List<String>> header : conn.getHeaderFields().entrySet()) { 
     System.out.println(header.getKey() + "=" + header.getValue()); 
    } 
} 

我看不出有什麼問題,但所有的返回是:

Status=[400 Bad Request] 
null=[HTTP/1.1 400 Bad Request] 
Server=[nginx] 
X-Request-Id=[ccc7a9a4a327b18ea4bf496f1f314fb8] 
X-Runtime=[0.032328] 
Connection=[keep-alive] 
X-MH-Cache=[appcache1; M] 
Date=[Sun, 06 Jul 2014 14:07:49 GMT] 
Via=[1.1 varnish] 
Accept-Ranges=[bytes] 
X-Varnish=[2778442693] 
X-UA-Compatible=[IE=Edge,chrome=1] 
Cache-Control=[max-age=0, private, must-revalidate] 
Vary=[Accept-Encoding] 
Content-Length=[83] 
Age=[0] 
X-API-Version=[2] 
Content-Type=[application/json; charset=utf-8] 

我想弄清楚這一個多星期了,我只是沒有看到這個錯誤。任何幫助將不勝感激。

+0

它與'GET'而不是'PUT'一起工作嗎? – kaetzacoatl

+0

GET請求工作是 – user3715497

+0

也許你的服務器不允許'PUT'請求。在這種情況下檢查您的服務器的代碼。 – kaetzacoatl

回答

0

嘗試檢查響應正文,因爲它可能包含有關拒絕的詳細信息。由於內容類型指定utf-8,則可以使用該創建的InputStreamReader:

try (Reader response = 
    new InputStreamReader(conn.getErrorStream(), StandardCharsets.UTF_8)) { 

    int c; 
    while ((c = response.read()) >= 0) { 
     System.out.print((char) c); 
    } 
} 

更新:響應身體指出「通道」參數不存在。這是因爲curl會自動將POST數據編碼爲application/x-www-form-urlencoded,但您的代碼不會。您需要在您的數據上使用URLEncoder,並設置請求的內容類型:

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
conn.setRequestMethod("PUT"); 
conn.setDoOutput(true); 
conn.setRequestProperty("Accept", "application/vnd.twitchtv.v2+json"); 
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

String data = "channel[status]=testing"; 
data = URLEncoder.encode(data, "UTF-8"); 
+0

好像相同的輸出'狀態= [400錯誤的請求] 空= [HTTP/1.1 400錯誤的請求] 服務器= [nginx的] X - 請求-ID = [2128f11fe37431f0e1e890f130b2b665] X-運行時= [0.039659] Connection = [keep-alive] X-MH-Cache = [appcache2; M] 日期= [太陽,2014年7月6日14時23分36秒GMT] 經由= [1.1清漆] 的Accept-範圍= [字節] X-光油= [1208902393] X-UA-兼容= [IE = [邊緣,鉻= 1] Cache-Control = [max-age = 0,private,must-revalidate] Vary = [Accept-Encoding] Content-Length = [0] Age = [0] X- API-Version = [2] Content-Type = [application/json; charset = utf-8] ' – user3715497

+0

這似乎不太可能;也許它沒有被刷新到標準輸出。嘗試在上面的代碼之後添加一個'System.out.println();'。標題指示響應正文包含83個字節的JSON。 – VGR

+0

哦,我的錯誤,它返回一個IO錯誤'在線程中的異常「主要」java.io.IOException:服務器返回的HTTP響應代碼:400爲URL:https://api.twitch.tv/kraken/channels/testacc222?oauth_token = 6e7b9cyfi8zk1gr8g06eecebnitlcvb \t在sun.net.www.protocol.http.HttpURLConnection.getInputStream0(未知來源) \t在sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知來源) \t在sun.net.www .protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source) \t at testing.twitch.main(twitch.java:31)' – user3715497