2010-08-16 16 views
2

J'essaie d'utiliser le httpbuilder groovy pour faire un post sur le service web Microsoft Exchange (EWS). Mon problème est, je suis incapable de définir le bon type de contenu de demande. La bibliothèque semble avoir son propre esprit ici.Comment spécifier le type de contenu dans la requête dans groovy?

Est-ce que quelqu'un a une idée?

Cheers, Stephan

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

Répondre

1

Eh bien, la lecture et le débogage du code, je trouve cela ma solution actuelle/solution. Pas aussi beau que je l'espérais:

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