gpt4 book ai didi

java - Java 中使用 commons HttpClient 的curl 等效项

转载 作者:行者123 更新时间:2023-11-30 04:18:58 26 4
gpt4 key购买 nike

我正在使用 Commons HttpClient 发送一个发布请求以及一些字符串内容作为参数。以下是我的代码:

    // obtain the default httpclient
client = new DefaultHttpClient();

// obtain a http post request object
postRequest = new HttpPost(stanbolInstance);
postRequest.setHeader("Accept", "application/json");

// create an http param containing summary of article
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", content));

try {
// add the param to postRequest
postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
// obtain the response
response = client.execute(postRequest);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

这里,stanbolInstance 是:http://dev.iks-project.eu:8081/enhancer这是行不通的。以下是异常(exception)情况:

Problem accessing /enhancer. Reason:
<pre> The parsed byte array MUST NOT be NULL!</pre></p><h3>Caused by:</h3><pre>java.lang.IllegalArgumentException: The parsed byte array MUST NOT be NULL!

以下是有效的 cURL 等效项:

curl -X POST -H "Accept: text/turtle" -H "Content-type: text/plain" --data "The Stanbol enhancer can detect famous cities such as Paris and people such as Bob Marley." http://dev.iks-project.eu:8081/enhancer

救命!

最佳答案

我认为您以错误的方式放置内容。

替换:

 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", content));

try {
// add the param to postRequest
postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

postRequest.setEntity(new StringEntity(content));

关于java - Java 中使用 commons HttpClient 的curl 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17600811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com