开发者

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

开发者 https://www.devze.com 2022-12-14 22:59 出处:网络
I\'m trying out the twitter streaming api. I could succesfully filter tweets by using curl, as stated here:

I'm trying out the twitter streaming api. I could succesfully filter tweets by using curl, as stated here:

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

where tracking is a plain file with the content:

track=Berlin

Now I tried to do the same thing in JavaSE, using Apache's HTTPComponents:

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(<user>, <pass>);
    DefaultHttpClient httpClient = new DefaultHttpClient();
    httpClient.getC开发者_运维问答redentialsProvider().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 that, I get:

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

as a single entry in my linkedQueue. Seems the api wants the parameter in a different form, but cannot find any hint in the documentation. Can somebody share some experiences with the api or see any other problem with the code? Thanks!

EDIT Putting the filter parameter into the params was a bad idea. As it's post data, it needs to be defined as an Entity before the request is being made:

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

That's what I was doing wrong. Thanks Brian!


I suspect you need to post the data as the contents of your HTTP post. The man page for curl -d says:

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.

so I believe you have to set that content type and put the contents of the tracking file in the body of your post.

0

精彩评论

暂无评论...
验证码 换一张
取 消