Java PostMethod 与 xml
我正在尝试将 XML 数据作为正文发布到 REST api。
我有一个创建请求的方法,称为 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);
}
和 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;
}
我的问题是 method.setRequestEntity 说找不到该方法。
我有
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.*;
If i set method = PostMethod 而不是 method = doREST 它可以工作,但我不想在所有其他方法中只为了创建查询而这样做。
我是否遗漏了为什么 method.setRequestEntity 无法按现在的方式工作?
编辑: 我从 PostMethod setRequestBody(String) 已弃用 - 为什么? 获得了使用 setRequestEntity 的信息
编辑 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;
}
I am trying to post XML data as a body to a REST api.
I have a method that creates the request called 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
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.
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.
Is there something I am missing as to why the method.setRequestEntity is not working the way it is right now?
EDIT:
I got my information for using setRequestEntity from PostMethod setRequestBody(String) deprecated - why?
EDIT 2:
Here is what I ended up doing.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该修改
doREST
以接受RequestEntity
而不是boolean
。为 GET 传入null
并为 POST 传入一个值。使用它来检查您是否需要PostMethod
或GetMethod
。然后您就可以拥有特定类型,以便您可以仅调用PostMethod
setRequestEntity()
。编辑:
您可以像这样避免演员阵容:
You should modify
doREST
to accept theRequestEntity
instead of aboolean
. Pass innull
for a GET and a value for POST. Use that as the check to see if you need aPostMethod
or aGetMethod
. Then you can have the specific type so you can call thePostMethod
onlysetRequestEntity()
.EDIT:
You can avoid the cast like this:
您应该调用的方法是
setEntity
,而不是setRequestEntity
。另外,您的身体应该包裹在StringEntity
,不是StringRequestEntity
,从org.apache.http.entity
导入。The method you should be calling is
setEntity
, notsetRequestEntity
. Also, your body should be wrapped in aStringEntity
, not aStringRequestEntity
, imported fromorg.apache.http.entity
.