Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于xml的Java后处理方法_Java_Apache Commons Httpclient - Fatal编程技术网

基于xml的Java后处理方法

基于xml的Java后处理方法,java,apache-commons-httpclient,Java,Apache Commons Httpclient,我试图将XML数据作为主体发布到RESTAPI 我有一个方法来创建名为doREST的请求 String url = null; HttpMethod method; LOG.info("QUERY: " + query); if (StringUtil.isEmpty(query)) { url = BuildRequestURL("/issues.ashx/issues/mywork"); meth

我试图将XML数据作为主体发布到RESTAPI

我有一个方法来创建名为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);
    }
我的问题是方法。setRequestEntity表示找不到该方法

我有

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.*;
如果我设置method=PostMethod而不是method=doREST,它会工作,但我不想在所有其他方法中只为创建查询而这样做

关于method.setRequestEntity为何不能按现在的方式工作,我是否缺少一些东西

编辑: 我从中获得了使用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;
    }

您应该调用的方法是,而不是
setRequestEntity
。此外,您的身体应该用从
org.apache.http.entity
导入的
StringRequestEntity
包装,而不是
StringRequestEntity
。您应该修改
doREST
以接受
RequestEntity
。传入GET的
null
和POST的值。将其用作检查,以查看是否需要
PostMethod
GetMethod
。然后您可以拥有特定的类型,这样您就可以只调用
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;
}

不要认为这会起作用,因为在doREST中,我需要返回PostMethod而不是HttpMethod。我采用了这种方法,但仍然存在问题,因此如果实体被传递,我最终将方法强制转换为PostMethod,然后设置setRequestEntity。您使用的是httpclient的哪个版本?在3.x(apache commons httpclient)和4.x(apache httpcomponents httpclient)之间有一些API命名更改
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;
}