我正在嘗試使用Twitch.tv API進行身份驗證並收到訪問令牌,但我無法查看響應的正文。爲什麼我的Java程序在試圖從SSL的Twitch.tv API獲取令牌時掛起?
public static void getToken(String body) {
try {
SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) sf.createSocket("api.twitch.tv", 443);
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.print("POST /kraken/oauth2/token HTTP/1.1\r\n");
pw.print("Host: api.twitch.tv\r\n");
pw.print("Content-Type: application/x-www-form-urlencoded\r\n");
pw.print("Content-Length: 189\r\n");
pw.print("Client-ID: [myClientIDHere]\r\n\r\n"); // Edited it out when posting online.
pw.print(body);
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
這是我迄今爲止的代碼。我得到的輸出是:
HTTP/1.1 400 Bad Request
Accept-Ranges: bytes
Access-Control-Allow-Headers:
Access-Control-Allow-Methods:
Access-Control-Allow-Origin: *
Age: 0
Cache-Control: no-cache, no-store, must-revalidate
Content-Type: application/json; charset=utf-8
Date: Sat, 05 Nov 2016 07:22:35 GMT
Pragma: no-cache
Server: nginx
Set-Cookie: unique_id=92143f722dd21b4e; domain=.twitch.tv; path=/; expires=Thu, 05-Nov-2026 07:22:35 GMT
Set-Cookie: _twitch_session_id=8c5ec350cad83ea4150e1dc05be029f3; domain=.twitch.tv; path=/; expires=Sat, 05-Nov-2016 19:22:35 GMT; secure; HttpOnly
Status: 400 Bad Request
Vary: Accept-Encoding
Via: 1.1 varnish
X-Frame-Options: SAMEORIGIN
X-MH-Cache: rails-varnish-api-aws-0538d8f099256d673; M
X-Request-Id: 6f6355698a19e94058d35af2df918adb
X-UA-Compatible: IE=Edge,chrome=1
X-Varnish: 3967202340
Content-Length: 75
Connection: keep-alive
然而,它會在那之後掛起而不再給我。正因爲如此,我無法調試爲什麼我收到不好的請求。有沒有人有任何想法,爲什麼這是? (這是我第一次發佈,抱歉,如果任何格式不正確)。
謝謝。