简体   繁体   中英

Why is my Java program is hanging when trying to get a token from the Twitch.tv API with SSL?

I am trying to authenticate with the Twitch.tv API and receive an access token, but I am having trouble being able to look at the body of the response.

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();           
    }

}

This is my code thus far. The output I get is:

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

However, it will just hang after that and not give me anymore. Because of this, I can't debug why I am getting the bad request. Does anyone have any ideas as to why this is? (This is my first time posting, sorry if any formatting is improper).

Thank you.

Probably the response body (json) doesn't use a line terminator . readLine will wait forever for a line terminator, or until the socket is closed, which won't happen promptly because you specified HTTP/1.1 and didn't override the default keep-alive, although the server may or may not choose to close at some arbitrary later time if you keep waiting.

When receiving an HTTP response, you should read the header as lines until the empty line that terminates the header, but for the body you must read only the number of bytes (not characters, which can be different in utf-8, and definitely not lines) specified in Content-length, or by the series of chunk headers if transfer-encoding chunked is enabled and used. Read rfc 2616, or if you have extra time to spend the newer and even larger 7230 and 7231.

Or better, use one of the very many libraries that already do this correctly, like the (Https)URLConnection provided in Java, or the popular Apache HttpClient library.

PS: Your problem almost certainly has nothing to do with SSL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM