最近c#開發調用java的webservice接口,調用過程當中老是報錯缺乏header驗證信息,與接口發佈方溝通,查看soap文件才知道原來有的接口方法須要加soapheader驗證,可是本地直接經過wsdl地址生成的代理客客戶端方法中是看不出來的。java
首先要理解webservice的原理,webservice的交互最終都是基於SOAP信息的交互的。web
網上一堆常見的方法我就不說了,這裏說說我找到的我的感受不錯的方法:c#
一種就是簡單粗暴的拼裝soap了,這是最直觀有效可是很麻煩的方法了。安全
public class InvokeServiceWithSoap { public static void InvokeService() { StringBuilder soap = new StringBuilder(); soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); soap.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:end=\"http://localhost/service/\">"); soap.Append("<soapenv:Header>"); soap.Append("<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"); soap.Append("<wsse:UsernameToken>"); soap.Append("<wsse:Username>username</wsse:Username>");//用戶名 soap.Append("<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">password</wsse:Password>");//口令 soap.Append("</wsse:UsernameToken>"); soap.Append("</wsse:Security>"); soap.Append("</soapenv:Header>"); soap.Append("<soapenv:Body>"); soap.Append("<end:service1>"); soap.Append("<arg0></arg0>"); soap.Append("</end:service1>"); soap.Append(" </soapenv:Body>"); soap.Append(" </soapenv:Envelope>"); string url = "http://localhost/end:service1"; var result = GetSOAPReSource(url, soap.ToString()); } public static string GetSOAPReSource(string url, string datastr) { try { //request Uri uri = new Uri(url); WebRequest webRequest = WebRequest.Create(uri); webRequest.ContentType = "text/xml; charset=utf-8"; webRequest.Method = "POST"; using (Stream requestStream = webRequest.GetRequestStream()) { byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString()); requestStream.Write(paramBytes, 0, paramBytes.Length); } //response WebResponse webResponse = webRequest.GetResponse(); using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { string result = ""; return result = myStreamReader.ReadToEnd(); } } catch (Exception ex) { throw ex; } } }
另一種方法是我找到的最簡單的,我就是使用了下面的方法:ui
直接使用.net中的服務引用,注意是服務引用(相似WCF引用)主要經過servicemodel來設置安全認證信息,framework3.0以上都支持,
引用後將 Config中 servicemodel 的 endpoint 節點 增長headers ,安全驗證信息增長儘可能來便可。
如下是完整的config:url
<system.serviceModel> <bindings> <basicHttpBinding> <binding name="Service1SoapBinding" maxReceivedMessageSize="99999999"/> </basicHttpBinding> </bindings> <client> <endpoint address="http://local:9090/ Service1" binding="basicHttpBinding" bindingConfiguration="onlineUserServiceSoapBinding" contract="smpwcf.Service1" name="Service1ImplPort" > <headers> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> <wsse:UsernameToken> <wsse:Username>username</wsse:Username> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password> </wsse:UsernameToken> </wsse:Security> </headers> </endpoint> </client> 21 </system.serviceModel>