2

I'm using MSXML2_TLB.pas generated from the Microsoft XML type library to call a pretty simple web-service, so I don't need the full XML wrapping and just do this:

var
  r:XMLHTTP;
begin
  r:=CoXMLHTTP.Create;
  r.open('POST',WebServiceURL,false,'','');
  r.setRequestHeader('Content-Type','application/xml');
  r.send('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" '+
    'xmlns:ns="http://somebigcorporation.com/api/ws1/">'+
    '<soapenv:Body>'+
    //snip: irrelevant to the question
    '</soapenv:Body></soapenv:Envelope>');

  if r.status<>200 then raise Exception.Create(r.statusText);
  Result:=(r.responseXML as DOMDocument).documentElement.firstChild.firstChild.selectSingleNode('importantData');
end;

The webservice responds nicely with status 200 and Content-Type: text/xml; charset=iso-8859-1.

In some cases, documentElement is nil (and the above code throws an access violation). Apparently responseXML exists, but only to offer a parseError object (it says so in the docs), and parseError.reason in these cases is An invalid character was found in text content..

Why is the parser not taking the 'charset' value from the response header? Is there a way to tell the XMLHTTP instance to use the charset value from the response header?

Update: I don't know if it's important, but the response also doesn't start with <?xml and even if it did I have no way to request/modify it so it would have an encoding="iso-8859-1" attribute.

Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67

1 Answers1

0

I think you need to set the charset in the request so the result is correct.

r.setRequestHeader('Content-Type', 'application/xml; charset=ISO-8859-1');
  • The result _is_ correct. The request is also handled correctly, I leave the request body encoding to the XMLHTTP object. I doubt setting the request content type will affect the response content type. It's the fact XMLHTTP apparently misinterpret's the response's encoding that is the problem. – Stijn Sanders Nov 14 '14 at 15:44
  • 1
    @StijnSanders, This is not the core of your problem, but in any case you *should* specify your request `Content-Type`. the web-service *might* be expecting (and handling) this. for example, if you specify a request with content-type of UTF8, your might be surprised to find out that the server response with UTF8 as well. this is true for ASMX .net web-services for examples. – kobik Nov 15 '14 at 11:59