c#调用带有安全认证的java webservice
最近使用c#调用注册局写的java webservice耽误了很多时间,网上找了很久发现了这篇帖子,觉得不错,分享给大家看看,希望对大家有帮助。
1.拼装soap使用http post ,主要将验证身份信息放入header中,以下code供参考:8-15行内用户、密码,其他soap信息需要根据自己的service修改,
可以使用soapui获取到body以及xmlns信息
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; } } }
2.使用wsdl生成代理类,代理类重写HttpRequest ,将安全验证信息增加到request中
protected override System.Net.WebRequest GetWebRequest(Uri uri) { System.Net.WebRequest request= base.GetWebRequest(uri); System.Net.NetworkCredential nc = new System.Net.NetworkCredential(); nc.UserName = "ssotest";//java webservice 用户名 nc.Password = "ssotest";//java webservice 密码 request.Credentials = nc; return request; }
下面说一个比较简单的方法:
直接使用.net中的服务引用,注意是服务引用(类似WCF引用)主要通过servicemodel来设置安全认证信息,framework3.0以上都支持,
引用后将 Config中 servicemodel 的 endpoint 节点 增加headers ,安全验证信息增加尽量来即可。
以下是完整的config:
<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>

浙公网安备 33010602011771号