Category Archives: alchemyapi

Getting org.apache.http.client.ClientProtocolException while calling AlchemyAPI

I'm getting org.apache.http.client.ClientProtocolException while calling AlchemyAPI for TextGetTargetedSentiment with Apache HttpClient:

org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response

Below is the code snippet.

URI uri = new URIBuilder()
    .setScheme("http")
    .setHost("access.alchemyapi.com/calls/text/TextGetTargetedSentiment")
    .setParameter("apikey", <api-key>)
    .setParameter("outputMode", "json")
    .setParameter("showSourceText", "0")
    .setParameter("target", <target>)
    .setParameter("text", <news article>)
    .build();

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(uri);

// add header
post.setHeader("User-Agent", <user agent>);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");

HttpResponse response = client.execute(post);

Thanks for your help.