簡體   English   中英

如何在Groovy中指定請求中的內容類型?

[英]How to specify content type in request in groovy?

我正在嘗試使用groovy httpbuilder向Microsoft Exchange Web服務(EWS)發布信息。 我的問題是,我無法設置正確的請求內容類型。 圖書館似乎在這里有自己的想法。

有人有主意嗎?

干杯,斯蒂芬

這是我的代碼:

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

好了,閱讀和調試代碼后,我發現這是我當前的解決方法/解決方案。 沒有我希望的那么美麗:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM