MSXML: XMLHTTP doesn't support charset from header

622 views Asked by At

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.

1

There are 1 answers

2
David Buterbaugh On

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');