2010-08-16 67 views
2

我正在嘗試使用groovy httpbuilder發佈到微軟交換web服務(EWS)。我的問題是,我無法設置正確的請求內容類型。圖書館似乎在這裏有自己的想法。如何在groovy中指定請求中的內容類型?

有沒有人有想法?

乾杯, 斯蒂芬

這裏是我的代碼:

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

回答

1

那麼,閱讀和調試代碼,我發現這是我目前的解決方法/解決方案。不如我所希望的美麗:

// 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 
} 
相關問題