简体   繁体   English

调用AlchemyAPI时获取org.apache.http.client.ClientProtocolException

[英]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: 我在使用Apache HttpClient调用AlchemyAPI for TextGetTargetedSentiment时遇到org.apache.http.client.ClientProtocolException

org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response org.apache.http.ProtocolException:服务器无法通过有效的HTTP响应进行响应

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. 如果此示例无法说明问题,请发送电子邮件至questions@alchemyapi.com,以获得更多支持。 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. 尝试关闭防火墙。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.apache.http.client.ClientProtocolException - org.apache.http.client.ClientProtocolException HttpPost 中的 org.apache.http.client.ClientProtocolException - org.apache.http.client.ClientProtocolException in HttpPost Apache HttpComponents:org.apache.http.client.ClientProtocolException - Apache HttpComponents: org.apache.http.client.ClientProtocolException HttpClient执行时出现org.apache.http.client.ClientProtocolException - org.apache.http.client.ClientProtocolException appears when HttpClient executes Unirest Java 客户端:kong.unirest.UnirestException:org.apache.http.client.ClientProtocolException - Unirest Java Client : kong.unirest.UnirestException: org.apache.http.client.ClientProtocolException Spring WS-I / O错误:null; 嵌套的异常是org.apache.http.client.ClientProtocolException - Spring WS - I/O error: null; nested exception is org.apache.http.client.ClientProtocolException 使用httpclient 4.1.2连接到https会产生org.apache.http.client.ClientProtocolException - connection to https with httpclient 4.1.2 gives org.apache.http.client.ClientProtocolException java.lang.NoClassDefFoundError:使用jsonparser()时org / apache / http / client / ClientProtocolException - java.lang.NoClassDefFoundError: org/apache/http/client/ClientProtocolException while using the jsonparser() 使用[org.apache.http.client] HttpClient 4.1.1首次获得未经授权的401 - Getting 401 unauthorized for the first time using [org.apache.http.client] HttpClient 4.1.1 获取导入无法解决导入 org.apache.http.client.methods.HttpGet 的错误 - getting a import cannot be resolved error for import org.apache.http.client.methods.HttpGet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM