gpt4 book ai didi

delphi - MSXML:XMLHTTP不支持 header 中的字符集

转载 作者:行者123 更新时间:2023-12-03 17:56:05 24 4
gpt4 key购买 nike

我正在使用从MSXML2_TLB.pas类型库生成的Microsoft XML来调用一个非常简单的Web服务,因此我不需要完整的XML包装,只需执行以下操作:

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;


Web服务以状态200和 Content-Type: text/xml; charset=iso-8859-1很好地响应。

在某些情况下, documentElement为nil(并且上面的代码引发访问冲突)。显然 responseXML存在,但仅提供一个 parseError对象(在 the docs中这样说),在这种情况下, parseError.reasonAn invalid character was found in text content.

为什么解析器不从响应头中获取'charset'值?有没有办法告诉XMLHTTP实例使用响应头中的字符集值?

更新:我不知道它是否重要,但是响应也不以 <?xml开头,即使响应也没有办法请求/修改,所以它将具有 encoding="iso-8859-1"属性。

最佳答案

我认为您需要在请求中设置字符集,以便结果正确。

r.setRequestHeader('Content-Type', 'application/xml; charset=ISO-8859-1');

关于delphi - MSXML:XMLHTTP不支持 header 中的字符集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26930932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com