Calls to Streaming API are blocking


#1

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

#2

Without any hint what programming language you are using and which HTTP Framework it is hard to help you. The issue is not with the Streaming API but with your Framework and how it handles the HTTP request. It seems it waits until all the body is received, which will never be the case, as it is a long-live connection.


#3

Programming language is Java and the framework used is HttpUrlConnection


#4

I have no experience with Java, but you might want to check out this blogpost.