2010-08-16 3 views
2

groovy httpbuilder를 사용하여 Microsoft Exchange 웹 서비스 (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 
}