Twitter streaming api - set filter in http post request (apache httpcomponents)

I am trying to use twitter streaming api. I could successfully filter tweets using curl, as indicated here :

curl -d @tracking http://stream.twitter.com/1/statuses/filter.json -u <user>:<pass>

where trackingis the usual file with the contents:

track=Berlin

Now I tried to do the same in JavaSE using Apache HTTPComponents:

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(<user>, <pass>);
    DefaultHttpClient httpClient = new DefaultHttpClient();
    httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
    HttpPost httpPost = new HttpPost("http://stream.twitter.com/1/statuses/filter.json");
    HttpParams params = new BasicHttpParams();

    params = params.setParameter("track", "Berlin");
    httpPost.setParams(params);

    try {
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            InputStream instream = entity.getContent();
            String t;
            BufferedReader br = new BufferedReader(new InputStreamReader(instream));
            while(true) {
                t = br.readLine();
                if(t != null) {                        
                    linkedQueue.offer(t);
                }
            }
        }
    } catch (IOException ioe) {
        System.out.println(ioe.getMessage());
    }
    finally{
        httpClient.getConnectionManager().shutdown();
    }

When I run this, I get:

No filter parameters found. Expect at least one parameter: follow track

like one entry in mine linkedQueue. It seems api wants the parameter in a different form, but cannot find any hint of documentation. Can someone share their experience with api or see any other problem with the code? Thanks!

. Entity , :

        StringEntity postEntity = new StringEntity("track=Berlin", "UTF-8");
        postEntity.setContentType("application/x-www-form-urlencoded");
        httpPost.setEntity(postEntity);

. !

+3
1

, HTTP-. curl -d :

(HTTP) POST- HTTP-, , , HTML . /-WWW--urlencoded.

, .

+3

Source: https://habr.com/ru/post/1726170/


All Articles