Java PostMethod 与 xml

发布于 2024-11-04 03:46:24 字数 2453 浏览 1 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£烟消云散 2024-11-11 03:46:24

您应该修改 doREST 以接受 RequestEntity 而不是 boolean。为 GET 传入 null 并为 POST 传入一个值。使用它来检查您是否需要 PostMethodGetMethod。然后您就可以拥有特定类型,以便您可以仅调用 PostMethod setRequestEntity()

编辑:

您可以像这样避免演员阵容:

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;
}

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().

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;
}
噩梦成真你也成魔 2024-11-11 03:46:24

您应该调用的方法是 setEntity,而不是 setRequestEntity。另外,您的身体应该包裹在 StringEntity,不是 StringRequestEntity,从 org.apache.http.entity 导入。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文