浅谈关于Webservices对接的一些问题

开发 后端
最近在和一些公司做Webservices对接,其中有.NET 调用.NET,也有.NET调用Java。这里为大家总结下这两种对接不同的处理方法。

.NET调用.NET写的Web服务

.NET和.NET的Webservices对接比较简单。只要知道对方的web服务编码文件(.asmx)或者web服务描述文件(.wsdl),在项目中添加web应用即可。

同理,如果是你为对方提供web服务,只要提供上面的文件即可。

安全性方面我们是用了下面两个方法,如果有其他方法,不妨一起讨论:

1、soapheader验证

  1. public class ProductSoapHeader : SoapHeader  
  2.     {  
  3.         public string Username;  
  4.         public string Password;  
  5.         public ProductSoapHeader() { }  
  6.         public ProductSoapHeader(string u, string p)  
  7.         {  
  8.             Username = u;  
  9.             Password = p;  
  10.         }  
  11.  } 

2、限制登入ip

  1. CustomerIP=HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] 

.NET调用Java写的web服务

.NET调用java也需要知道它的描述文件地址,具体有以下几种调用方法:

a)在项目中添加web应用。

b)使用wsdl.exe将wsdl文件编译成动态库,这样使用起来会更加方便。

b.1)生成类文件

  1. wsdl.exe /l:cs /n:webser /out:C:/webser.cs c:/test.wsdl 

b.2)生成动态库

  1. csc /target:library /out:"c:\webser.dll" c:\webser.cs 

生成动态库

c)直接sent SOAP request

如果对方提供了SOAP request的格式,这无疑是最直接的方法。

下面提供一个发送SOAP请求的示例:

  1. private void sendSoap()  
  2.     {  
  3.  
  4.         XmlDocument xmldoc = new XmlDocument();  
  5.         xmldoc.Load(Server.MapPath("user.xml"));  
  6.         string data = xmldoc.InnerXml;  
  7.         string url = "XXX";  
  8.         string result=null;  
  9.         getResponse(url, data, ref result);  
  10.         //others  
  11.  
  12.     }  
  13. }  
  14.    ///   
  15.     /// 发送SOAP请求  
  16.     ///   
  17.     /// 地址  
  18.     /// 请求内容  
  19.     /// 返回结果  
  20.     public void getResponse(string url, string datas, ref string result)  
  21.     {  
  22.         ASCIIEncoding encoding = new ASCIIEncoding();  
  23.         byte[] data = encoding.GetBytes(datas);  
  24.         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);  
  25.         request.AllowAutoRedirect = true;  
  26.         request.Method = "POST";  
  27.         request.ContentType = "text/xml; charset=utf-8";  
  28.         request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";  
  29.         request.ContentLength = data.Length;  
  30.         Stream stream = request.GetRequestStream();  
  31.         stream.Write(data, 0, data.Length);  
  32.         stream.Close();  
  33.         HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
  34.         StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);  
  35.         result = reader.ReadToEnd();  
  36.         reader.Close();  
  37.     } 

一个SOAP格式的例子 

  1. < ?xml version="1.0" encoding="UTF-8"?> 
  2. < soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
  3. < soap:Body> 
  4.    < e:RegisterUser xmlns:e="XXX"> 
  5.     < UserInfo> 
  6.      < email>XXX  
  7.      < fname>XXX  
  8.      < lname>XXX  
  9.      < password>XXX  
  10.     < /UserInfo> 
  11.    < /e:RegisterUser> 
  12. < /soap:Body> 
  13. < /soap:Envelope> 

.NET里默认的SOAP格式:

  1. < ?xml version="1.0" encoding="utf-8"?> 
  2. < soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.                xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  4.                xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
  5.   < soap:Body> 
  6.     < DocumentWrappedLiteral xmlns="http://www.contoso.com"> 
  7.       < MyAddress> 
  8.         < Street>string  
  9.         < City>string  
  10.         < Zip>string  
  11.       < /MyAddress> 
  12.       < useZipPlus4>boolean  
  13.     < /DocumentWrappedLiteral> 
  14.   < /soap:Body> 
  15. < /soap:Envelope> 

msdn上的一篇文章讲如何:控制 Web 服务方法的总体 SOAP Body 的格式设置

最近在做一些接口方面的工作,肯定还有很多没顾及到的东西。也希望大家能多讨论一些。

【编辑推荐】

  1. WebServices返回数据的4种方法横向比较
  2. 利用C#指针进行图像操作
  3. C# 4.0中泛型协变性和逆变性详解
  4. C#实例讲解二叉树原理与实现
  5. Ajax,未来的WebServices?
责任编辑:彭凡 来源: cnblogs
相关推荐

2016-10-18 22:10:02

HTTP推送HTML

2009-07-21 10:35:18

margin coll

2018-06-12 15:39:41

容器部署云平台

2021-10-21 06:52:17

Vue3组件 API

2011-11-01 09:29:08

Android 4.0

2009-11-30 13:51:28

VS2003 Runt

2011-05-31 17:50:07

白盒测试

2011-03-08 14:28:03

proftpdGentoo

2018-05-17 14:52:11

Javascripthtmlcss

2022-01-16 08:04:44

集群部署canal

2009-08-06 16:01:30

C#接口成员

2009-06-10 21:46:02

JavaScript与

2010-09-17 15:41:46

网络协议分析软件

2010-05-04 15:59:05

Oracle字符集

2012-12-19 11:40:13

思科路由器

2009-11-23 13:44:33

PHP5面向对象

2011-01-26 16:24:53

Sun甲骨文

2009-06-18 15:14:53

Spring osgi

2009-06-04 16:28:43

EJB常见问题

2012-04-25 22:45:46

点赞
收藏

51CTO技术栈公众号