• 关于webservices的一些问题


    最近在和一些公司做webservices对接,其中有.NET 调用.NET,也有.NET调用Java。

    .NET调用.NET写的web服务

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

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

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

    1、soapheader验证

     public class ProductSoapHeader : SoapHeader
        {
            
    public string Username;
            
    public string Password;
            
    public ProductSoapHeader() { }
            
    public ProductSoapHeader(string u, string p)
            {
                Username 
    = u;
                Password 
    = p;
            }
     }

    2、限制登入ip

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

    .NET调用Java写的web服务

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

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

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

       b.1)生成类文件

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

     b.2)生成动态库

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


    c)直接sent SOAP request

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

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

       

    private void sendSoap()
        {

            XmlDocument xmldoc 
    = new XmlDocument();
            xmldoc.Load(Server.MapPath(
    "user.xml"));
            
    string data = xmldoc.InnerXml;
            
    string url = "XXX";
            
    string result=null;
            getResponse(url, data, 
    ref result);
            
    //others

        }
    }
       
    /// <summary>
        
    /// 发送SOAP请求
        
    /// </summary>
        
    /// <param name="url">地址</param>
        
    /// <param name="datas">请求内容</param>
        
    /// <param name="result">返回结果</param>
        public void getResponse(string url, string datas, ref string result)
        {
            ASCIIEncoding encoding 
    = new ASCIIEncoding();
            
    byte[] data = encoding.GetBytes(datas);
            HttpWebRequest request 
    = (HttpWebRequest)WebRequest.Create(url);
            request.AllowAutoRedirect 
    = true;
            request.Method 
    = "POST";
            request.ContentType 
    = "text/xml; charset=utf-8";
            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)";
            request.ContentLength 
    = data.Length;
            Stream stream 
    = request.GetRequestStream();
            stream.Write(data, 
    0, data.Length);
            stream.Close();
            HttpWebResponse response 
    = (HttpWebResponse)request.GetResponse();
            StreamReader reader 
    = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            result 
    = reader.ReadToEnd();
            reader.Close();
        }


    -------------------------------补充-------------------------------------

    一个SOAP格式的例子

    <?xml version="1.0" encoding="UTF-8"?>
    <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/">
    <soap:Body>
       
    <e:RegisterUser xmlns:e="XXX">
        
    <UserInfo>
         
    <email>XXX</email>
         
    <fname>XXX</fname>
         
    <lname>XXX</lname>
         
    <password>XXX</password>
        
    </UserInfo>
       
    </e:RegisterUser>
    </soap:Body>
    </soap:Envelope>
    .net里默认的SOAP格式:

    <?xml version="1.0" encoding="utf-8"?>
    <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/">
      
    <soap:Body>
        
    <DocumentWrappedLiteral xmlns="http://www.contoso.com">
          
    <MyAddress>
            
    <Street>string</Street>
            
    <City>string</City>
            
    <Zip>string</Zip>
          
    </MyAddress>
          
    <useZipPlus4>boolean</useZipPlus4>
        
    </DocumentWrappedLiteral>
      
    </soap:Body>
    </soap:Envelope>
    msdn上的一篇文章讲如何:控制 Web 服务方法的总体 SOAP Body 的格式设置

    ------------------------------------------------------------------------------

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

  • 相关阅读:
    php Composer 报ssl证书错误
    centos svn服务器安装
    nginx配置 php 单入口
    centos nginx 安装
    Zend Studio的配置和使用
    不要让别人笑你不能成为程序员
    10个用于Web开发的最好 Python 框架
    分析和解析PHP代码的7大工具
    php模拟用户自动在qq空间发表文章的方法
    PHP中实现MySQL嵌套事务的两种解决方案
  • 原文地址:https://www.cnblogs.com/tenghoo/p/1501495.html
Copyright © 2020-2023  润新知