I’ve set up a client which listens to the Streaming API for specific tweets, but are unable to get any responses from the HttpUrlConnection. Both conn.getResponseCode() and conn.getInputStream() are blocking until the connection times out. However if i change the request so that the OAuth signature provided is invalid conn.getResponseCode() returns 401 and I am able to read an Unauthorized message from the input stream. Any tips on how to get it working?
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(METHOD);
connection.setReadTimeout(600000);
connection.setRequestProperty("Accept", "*/*");
Map<String, String> map = getParamMap(consumerKey, nonce, OAUTH_METHOD, timestamp, token, OAUTH_VERSION);
String signature;
try {
signature = TwitterListenerUtils.getOauthSignature(QUERY_STRING, map, path, METHOD, REQUEST_URL, consumerSecret, secret);
} catch (InvalidKeyException | NoSuchAlgorithmException | DecoderException e) {
log.warning("Exception when generating oauth signature!");
log.warning("Class: " + e.getClass().getName());
log.warning("Message: " + e.getMessage());
return;
}
String auth = TwitterListenerUtils.generateHeader(consumerKey, nonce, signature, OAUTH_METHOD, timestamp, token, OAUTH_VERSION);
log.info("Auth: " + auth);
connection.setRequestProperty("Authorization", auth);
log.info("Sat auth");
connection.setDoOutput(true);
log.info("Sat doOutput");
PrintWriter writer = new PrintWriter(connection.getOutputStream());
log.info("Opened PrintWriter");
writer.write("track=" + TAGS);
log.info("Wrote tags");
writer.close();
log.info("Closed PrintWriter");
connection.connect();
log.info("Connected");
//parseStream(connection);
//log.info("Avail: " + connection.getInputStream().available());
log.info("Code: " + connection.getResponseCode());
log.info("Msg: " + connection.getResponseMessage());