本文介绍了如何在groovy中指定请求中的内容类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用groovy httpbuilder发布到微软Exchange Web服务(EWS)。我的问题是,我无法设置正确的请求内容类型。图书馆似乎在这里有自己的想法。



有人有想法吗?

干杯,
Stephan



这是我的代码:

  url =http:// exchangeserver / ews / Exchange。 asmx
p_body =< soap request> ...;
p_contentType =text / xml; charset = utf-8
customHeaders = [SOAPAction:LONG_URL]
$ b $ def http http http new HTTPBuilder(url);
http.auth.basic(authMap.username,authMap.password)

// contentType:p_contentType,
http.request(POST)
{
contentType = ContentType.TEXT //我们不想得到解析的响应
headers ['Accept'] =* / *; //只需确保我们接受所有内容

//支持额外的头文件
(customHeaders中的x){
头文件[x] = customHeaders [x]
}


/// Exchange需要text / xml; charset = utf-8而不是其他的东西:(

//发送文本/纯文本
// body = p_body
// requestContentType = p_contentType

//这会发送application / xml,而不是我的text / xml; charset = utf-8content-type。
发送p_contentType,p_body

//成功请求应该被记录;)
response.success = {resp,xml - >
println xml
}
}


解决方案好吧,阅读和调试代码,我发现这是我目前的解决方法。不像我所希望的那样美丽:

  //我们覆盖默认的文本/ xml编码器
//因为它用'application / xml'替换我们的contentType
//但是Exchange只喜欢'text / xml; charset = utf-8'
http.encoder.'text / xml'= {
body - > def se = new StringEntity(body,utf-8)
se.setContentType(text / xml; charset = utf-8)
return se
}


i'm trying to use the groovy httpbuilder to make a post to the microsoft exchange webservice (EWS). My problem is, I'm unable to set the proper request content-type. The library seems to have its own mind here.

Does anyone have an idea?

Cheers, Stephan

Here is my code:

    url = "http://exchangeserver/ews/Exchange.asmx"
    p_body = "<soap request >...";
    p_contentType = "text/xml; charset=utf-8"
    customHeaders = ["SOAPAction":"LONG_URL"]

    def http = new HTTPBuilder(url);
    http.auth.basic(authMap.username, authMap.password)

    // contentType: p_contentType,
    http.request( POST )
    {
        contentType = ContentType.TEXT // We dont want to get the response parsed
        headers['Accept'] = "*/*"; // Just make sure we accept everything

        // Support additional headers
        for (x in customHeaders) {
            headers[x] = customHeaders[x]
        }


        /// Exchange expects "text/xml; charset=utf-8" and nothing else :(

//  This sends text/plain
//      body = p_body
//      requestContentType = p_contentType

        // This sends application/xml, not my "text/xml; charset=utf-8" content-type.
            send p_contentType, p_body

        // a successfull request should be "logged" ;)
        response.success = { resp, xml ->
            println xml
        }
    }
解决方案

Well, reading and debugging the code, I found this to be my current workaround / solution. Not as beautifull as I hoped:

// We overwrite the default text/xml encoder,
// because it replaces our contentType with 'application/xml'
// But Exchange only likes 'text/xml; charset=utf-8'
http.encoder.'text/xml' = {
    body -> def se = new StringEntity(body, "utf-8")
    se.setContentType("text/xml; charset=utf-8")
    return se
}

这篇关于如何在groovy中指定请求中的内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 06:54