Delphi 调用WebService 直接发送SOPA报文
有一些WebService使用D7的HTTPRIO控件已经不能正常调用了,
这个时候可以直接使用HTTPReqResp控件直接发送报文,
或者使用TIdHTTP控件 http post方式请求 webservicessoap协议接口
函数示例:
HTTPReq: THTTPReqResp;
function TDM_Demo.WebSrvMain( In_fkey_XML,In_funccode,In_fparam_XML:String):String;
var
DataMsg:WideString;
strStream, strSend: TStringStream;
begin
//使用SOPAUI等工具获得标准示例报文DataMsg
DataMsg:='<?xml version="1.0" encoding="utf-8"?>'+
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"'+
' xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '+
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '+
'xmlns:xsd="http://www.w3.org/2001/XMLSchema">'+
'<SOAP-ENV:Body>'+
'<m:funMain xmlns:m="http://services.dtshis/">'+
'<In_fkey_XML><![CDATA['+In_fkey_XML+']]></In_fkey_XML>'+
'<In_funccode>'+In_funccode+'</In_funccode>'+
'<In_fparam_XML><![CDATA['+In_fparam_XML+']]></In_fparam_XML>'+
'</m:funMain></SOAP-ENV:Body></SOAP-ENV:Envelope>';
strStream := TStringStream.Create('');
strSend := TStringStream.Create(AnsiToUtf8(DataMsg));//Utf8转码
try
HTTPReq.url := '服务地址';
HTTPReq.Execute(strSend, strStream);
result := strStream.DataString;
result := UTF8Decode(result);
//xml < > " 转码
result := StringReplace(result,'<','<',[rfReplaceAll, rfIgnoreCase]);
result := StringReplace(result,'>','>',[rfReplaceAll, rfIgnoreCase]);
result := StringReplace(result,'"','"',[rfReplaceAll, rfIgnoreCase]);
//转为delphi XMLDocument 文档
result := StringReplace(result,'<?xml version="1.0" encoding="UTF-8"?>',
'<?xml version="1.0" encoding="gb2312"?>',[rfReplaceAll, rfIgnoreCase]);
finally
strStream.Free;
strSend.Free;
end;
end;
http post方式请求 webservices soap协议接口
with IdHttp do
begin
ProtocolVersion := pv1_1;
AllowCookies := True;
ProxyParams.BasicAuthentication := False;
ProxyParams.ProxyPort := 0;
Request.ContentLength := -1;
Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
Request.BasicAuthentication := False;
Request.ContentType := 'application/x-www-form-urlencoded';
HTTPOptions :=[hoKeepOrigProtocol, hoInProcessAuth];
ReadTimeout:=5000;
end;
strSend.WriteString(DataMsg);
result:=IdHttp.Post(sWSDLAddr,strSend);