基于之前知识:webservice就是约定提交和返回数据格式的http请求。那个不仅可以用DotNetCore的HttpClient调用带密码的webservice,用js也能请求webservice。
JS代码
CallWebservice("http://127.0.0.1:57772/imedicallis/csp/LIS.WS.DHCLISService.cls", "_system", "SYS", "Service.LIS.QC.DHCQCService","GetMachineList","","");
//调用webservice
function CallWebservice(Address, UserName, UserPass, ClassName, FuncName, Param, Session)
{
var xml = '<?xml version="1.0" encoding="utf-8"?>';
xml += '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">';
xml += '<soap:Header>';
xml += '<wsa:Action>http://tempuri.org/LIS.WS.DHCLISService.GetData</wsa:Action>';
xml += '<wsa:ReplyTo>';
xml += '<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>';
xml += '</wsa:ReplyTo>';
xml += '<wsa:To>' + Address + '</wsa: To >';
xml += '<wsse:Security soap:mustUnderstand="1">';
xml += '<wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-29db7c92-1098-4717-b7fb-aac473632fec">';
xml += '<wsse:Username>' + UserName + '</wsse:Username>';
xml += '<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' + UserPass + '</wsse:Password>';
xml += '</wsse:UsernameToken>';
xml += '</wsse:Security>';
xml += '</soap:Header>';
xml += '<soap:Body>';
xml += '<GetData xmlns ="http://tempuri.org">';
xml += '<ClassName>" + ClassName + "</ClassName>';
xml += '<FuncName >" + FuncName + "</FuncName>';
xml += '<Param ><![CDATA[" + Param + "]]></Param>';
xml += '<Session >' + Session + '</Session>';
xml += '</GetData >';
xml += '</soap:Body>';
xml += '</soap:Envelope>';
$.ajax({
headers: { SOAPAction: 'http://tempuri.org/LIS.WS.DHCLISService.GetData' },
contentType: 'application/soap+xml;charset=UTF-8;action="http://tempuri.org/LIS.WS.DHCLISService.GetData"',
dataType: 'xml',
type: 'post',
crossDomain: true,
xhrFields: {
withCredentials: true
},
url: Address,
data: xml,
success: function (data) {
var ss = $(data).find("GetDataResult").first().text();
alert(ss);
}
});
}
webservice输出允许跨域的头,也能用js调用带密码验证的webservice