带有 xml 的 Java PostMethod
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5834694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java PostMethod with xml
提问by Bot
I am trying to post XML data as a body to a REST api.
我正在尝试将 XML 数据作为主体发布到 REST api。
I have a method that creates the request called doREST.
我有一个方法可以创建名为 doREST 的请求。
String url = null;
HttpMethod method;
LOG.info("QUERY: " + query);
if (StringUtil.isEmpty(query)) {
url = BuildRequestURL("/issues.ashx/issues/mywork");
method = doREST(url, false);
} else {
url = BuildRequestURL("/issues.ashx/issues/filters");
//method = doREST(url, true);
method = doREST(url, true);
String xml = "<IssuesFilterEN>" +
"<IssueID>" + query + "</IssueID>" +
"</IssuesFilterEN>";
RequestEntity entity = new StringRequestEntity(xml,"text/xml; charset=iso-8859-1", null);
method.setRequestEntity(entity);
}
and the doREST method
和 doREST 方法
private HttpMethod doREST(String request, boolean post) throws Exception {
String uri = request;
HttpMethod method = post ? new PostMethod(uri) : new GetMethod(uri);
configureHttpMethod(method);
HttpClient client = getHttpClient();
client.getParams().setParameter(HttpClientParams.SO_TIMEOUT, timeoutLength);
client.executeMethod(method);
return method;
}
My issue is the method.setRequestEntity is saying that the method could not be found.
我的问题是 method.setRequestEntity 说找不到该方法。
I have
我有
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.*;
If i set method = PostMethod instead of method = doREST it works but I don't want to have to do that in all my other methods just to create queries.
如果我设置 method = PostMethod 而不是 method = doREST 它可以工作,但我不想在所有其他方法中这样做只是为了创建查询。
Is there something I am missing as to why the method.setRequestEntity is not working the way it is right now?
关于为什么 method.setRequestEntity 没有按照现在的方式工作,我有什么遗漏吗?
EDIT: I got my information for using setRequestEntity from PostMethod setRequestBody(String) deprecated - why?
编辑:我从PostMethod setRequestBody(String) 中获得了使用 setRequestEntity 的信息- 为什么?
EDIT 2: Here is what I ended up doing.
编辑 2:这是我最终要做的。
private HttpMethod doREST(String request, RequestEntity entity) throws Exception {
String uri = request;
HttpMethod method;
if ( entity != null ){
method = new PostMethod(uri);
((PostMethod) method).setRequestEntity(entity);
} else {
method = new GetMethod(uri);
}
configureHttpMethod(method);
HttpClient client = getHttpClient();
client.getParams().setParameter(HttpClientParams.SO_TIMEOUT, timeoutLength);
client.executeMethod(method);
return method;
}
回答by laz
You should modify doREST
to accept the RequestEntity
instead of a boolean
. Pass in null
for a GET and a value for POST. Use that as the check to see if you need a PostMethod
or a GetMethod
. Then you can have the specific type so you can call the PostMethod
only setRequestEntity()
.
您应该修改doREST
以接受RequestEntity
而不是boolean
。传入null
GET 和 POST 值。将其用作检查以查看您是否需要 aPostMethod
或GetMethod
. 然后您可以拥有特定类型,以便您可以调用PostMethod
唯一的setRequestEntity()
.
EDIT:
编辑:
You can avoid the cast like this:
你可以避免这样的演员阵容:
private HttpMethod doREST(String request, RequestEntity entity) throws Exception {
String uri = request;
HttpMethod method;
if ( entity != null ){
PostMethod postMethod = new PostMethod(uri);
postMethod.setRequestEntity(entity);
method = postMethod;
} else {
method = new GetMethod(uri);
}
configureHttpMethod(method);
HttpClient client = getHttpClient();
client.getParams().setParameter(HttpClientParams.SO_TIMEOUT, timeoutLength);
client.executeMethod(method);
return method;
}
回答by ataylor
The method you should be calling is setEntity
, not setRequestEntity
. Also, your body should be wrapped in a StringEntity
, not a StringRequestEntity
, imported from org.apache.http.entity
.
您应该调用的方法是setEntity
,而不是setRequestEntity
。此外,你的身体应该被包裹在一个StringEntity
,而不是一个StringRequestEntity
,从进口org.apache.http.entity
。