C#访问Java的WebService添加Header验证的问题
2016-08-12 00:00
393 查看
最近c#开发调用java的webservice接口,调用过程中总是报错缺少header验证信息,与接口发布方沟通,查看soap文件才知道原来有的接口方法需要加soapheader验证,但是本地直接通过wsdl地址生成的代理客客户端方法中是看不出来的。
首先要理解webservice的原理,webservice的交互最终都是基于SOAP信息的交互的。
网上一堆常见的方法我就不说了,这里说说我找到的个人感觉不错的方法:
一种就是简单粗暴的拼装soap了,这是最直观有效但是很麻烦的方法了。
另外一种方法是我找到的最简单的,我就是使用了下面的方法:
直接使用.net中的服务引用,注意是服务引用(类似WCF引用)主要通过servicemodel来设置安全认证信息,framework3.0以上都支持,
引用后将 Config中 servicemodel 的 endpoint 节点 增加headers ,安全验证信息增加尽量来即可。
以下是完整的config:
首先要理解webservice的原理,webservice的交互最终都是基于SOAP信息的交互的。
网上一堆常见的方法我就不说了,这里说说我找到的个人感觉不错的方法:
一种就是简单粗暴的拼装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; } } }
另外一种方法是我找到的最简单的,我就是使用了下面的方法:
直接使用.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>
相关文章推荐
- C#访问Java的WebService添加SOAPHeader验证的问题
- C#访问Java的WebService添加SOAPHeader验证的问题
- C# 调用jave中ajax的WebService引用后添加HTTP Header的问题
- 求救:C# 调用JAVA写的WEBSERVICE的问题!
- C++,C#,JAVA之间webservice互操作问题探讨
- java中multipart/form-data方式提交表单添加验证问题
- 解决.NET WebService引用后添加HTTP Header的问题
- Android访问C#的WebService要注意的问题
- Java和C#访问同一个数据库遇到的一些问题
- 关于java中Axis2客户端调用C#webservice服务端包含Cookie问题
- C# 调用JAVA(使用axis)开发的webservice 提示错误的问题
- 【转】用C#.NET调用Java开发的WebService传递int,double问题,出现java无法获得值!
- org.apache.commons.httpclient 访问需要验证的webservice的一些问题
- 用C#.NET调用Java开发的WebService传递int,double问题,出现java无法获 3ff0
- axis1.0+带证书验证方式访问webservice的问题
- 用C#.NET调用Java开发的WebService传递int,double问题,出现java无法获得值!
- Android 访问Webservice接口,参数对象不能串行化问题解决(java.lang.RuntimeException: Cannot serialize)
- C++,C#,JAVA之间webservice互操作问题探讨
- C++,C#,JAVA之间webservice互操作问题探讨
- 用C#.NET调用Java开发的WebService传递int,double问题,出现java无法获得值!