reficio / soap-ws

Java library, based on Spring-WS, that enables handling SOAP on a purely XML level
297 stars 145 forks source link

Soap client using StringEntity with default characterset and not UTF-8 #21

Open hawkd2006 opened 10 years ago

hawkd2006 commented 10 years ago

The SoapClient class is using the StringEntity constructor without a defined characterset, if you now want to send some UTF-8 characters the post execute will fail with the error message UnsupportedEncodingException. This is why the default is set to an ISO-characterset, if no characterset is defined. So I suggest to use the UTF-8 character set as a default in your code to avoid UnsupportedEncodingExceptions especially when your SoapRequest contains some special characters like in german language (p.e. ÄÜÖäüö).

// ----------------------------------------------------------------
// TRANSMISSION API
// ----------------------------------------------------------------
private HttpPost generatePost(String soapAction, String requestEnvelope) {
    try {
        HttpPost post = new HttpPost(endpointUri.toString());
        // ----------------------------------------------------------------
        // This is the changed code to avoid using ISO characterset.
        StringEntity contentEntity = new StringEntity(requestEnvelope, "UTF-8");
        // ----------------------------------------------------------------            
       post.setEntity(contentEntity);
        if (requestEnvelope.contains(SOAP_1_1_NAMESPACE)) {
            soapAction = soapAction != null ? "\"" + soapAction + "\"" : "";
            post.addHeader(PROP_SOAP_ACTION_11, soapAction);
            post.addHeader(PROP_CONTENT_TYPE, MIMETYPE_TEXT_XML);
            client.getParams().setParameter(PROP_CONTENT_TYPE, MIMETYPE_TEXT_XML);
        } else if (requestEnvelope.contains(SOAP_1_2_NAMESPACE)) {
            String contentType = MIMETYPE_APPLICATION_XML;
            if (soapAction != null) {
                contentType = contentType + PROP_DELIMITER + PROP_SOAP_ACTION_12 + "\"" + soapAction + "\"";
            }
            post.addHeader(PROP_CONTENT_TYPE, contentType);
        }
        return post;
    } catch (UnsupportedEncodingException ex) {
        throw new SoapClientException(ex);
    }
}
tombujok commented 10 years ago

Hi. Thanks! I will adjust it! If you want to you can submit a pull request, then your commit will be visible as a contribution on github.

dhara04-1 commented 9 years ago

Is this fixed by any chance? Facing same issue and need a fix for this.