简体   繁体   中英

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.

I suspect that there may be something odd about the values you are using for target and text that might be screwing things up. If this example doesn't clarify things you should email questions@alchemyapi.com for further support. The following works for me:

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Main {

 public static void main(String[] args) {
 try
 {
 HttpClient client = new DefaultHttpClient();

 String text = "Text analytics is fun with AlchemyAPI.";

 String API_KEY = ""; // Add your API key here

 URI uri = new URIBuilder()
 .setScheme("http")
 .setHost("access.alchemyapi.com/calls/text/TextGetTargetedSentiment")
 .setParameter("apikey", API_KEY)
 .setParameter("outputMode", "json")
 .setParameter("showSourceText", "1")
 .setParameter("target", "Text analytics")
 .setParameter("text", text)
 .build();

 HttpPost post = new HttpPost(uri);
 //the following headers aren't necessary for getting a response
 post.setHeader("User-Agent", "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0");
 post.setHeader("Content-Type", "application/x-www-form-urlencoded");

 System.out.println( "executing request " + post.getRequestLine());


 HttpResponse response = client.execute(post);
 String xmlString = EntityUtils.toString(response.getEntity());

 System.out.println(xmlString);
 } catch (UnsupportedEncodingException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (ClientProtocolException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
 }
}

I have a similar issue, and in my case it is a firewall-issue; it closes the connection because it rightfully finds it suspicious. Try switching off your firewall.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM