自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談關(guān)于Webservices對(duì)接的一些問題

開發(fā) 后端
最近在和一些公司做Webservices對(duì)接,其中有.NET 調(diào)用.NET,也有.NET調(diào)用Java。這里為大家總結(jié)下這兩種對(duì)接不同的處理方法。

.NET調(diào)用.NET寫的Web服務(wù)

.NET和.NET的Webservices對(duì)接比較簡(jiǎn)單。只要知道對(duì)方的web服務(wù)編碼文件(.asmx)或者web服務(wù)描述文件(.wsdl),在項(xiàng)目中添加web應(yīng)用即可。

同理,如果是你為對(duì)方提供web服務(wù),只要提供上面的文件即可。

安全性方面我們是用了下面兩個(gè)方法,如果有其他方法,不妨一起討論:

1、soapheader驗(yàn)證

  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調(diào)用Java寫的web服務(wù)

.NET調(diào)用java也需要知道它的描述文件地址,具體有以下幾種調(diào)用方法:

a)在項(xiàng)目中添加web應(yīng)用。

b)使用wsdl.exe將wsdl文件編譯成動(dòng)態(tài)庫,這樣使用起來會(huì)更加方便。

b.1)生成類文件

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

b.2)生成動(dòng)態(tài)庫

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

生成動(dòng)態(tài)庫

c)直接sent SOAP request

如果對(duì)方提供了SOAP request的格式,這無疑是最直接的方法。

下面提供一個(gè)發(fā)送SOAP請(qǐng)求的示例:

  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.     /// 發(fā)送SOAP請(qǐng)求  
  16.     ///   
  17.     /// 地址  
  18.     /// 請(qǐng)求內(nèi)容  
  19.     /// 返回結(jié)果  
  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.     } 

一個(gè)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里默認(rèn)的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 服務(wù)方法的總體 SOAP Body 的格式設(shè)置

最近在做一些接口方面的工作,肯定還有很多沒顧及到的東西。也希望大家能多討論一些。

【編輯推薦】

  1. WebServices返回?cái)?shù)據(jù)的4種方法橫向比較
  2. 利用C#指針進(jìn)行圖像操作
  3. C# 4.0中泛型協(xié)變性和逆變性詳解
  4. C#實(shí)例講解二叉樹原理與實(shí)現(xiàn)
  5. Ajax,未來的WebServices?
責(zé)任編輯:彭凡 來源: cnblogs
相關(guān)推薦

2016-10-18 22:10:02

HTTP推送HTML

2009-07-21 10:35:18

margin coll

2018-06-12 15:39:41

容器部署云平臺(tái)

2021-10-21 06:52:17

Vue3組件 API

2009-11-30 13:51:28

VS2003 Runt

2011-05-31 17:50:07

白盒測(cè)試

2011-11-01 09:29:08

Android 4.0

2018-05-17 14:52:11

Javascripthtmlcss

2011-03-08 14:28:03

proftpdGentoo

2022-01-16 08:04:44

集群部署canal

2009-08-06 16:01:30

C#接口成員

2010-09-17 15:41:46

網(wǎng)絡(luò)協(xié)議分析軟件

2010-05-04 15:59:05

Oracle字符集

2012-12-19 11:40:13

思科路由器

2009-06-10 21:46:02

JavaScript與

2009-06-04 16:28:43

EJB常見問題

2009-06-18 15:14:53

Spring osgi

2009-11-23 13:44:33

PHP5面向?qū)ο?/a>

2011-01-26 16:24:53

Sun甲骨文

2012-04-25 22:45:46

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)