65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 ASP.NET 调用 Web 服务

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (6投票s)

2007年12月27日

CPOL

4分钟阅读

viewsIcon

69150

downloadIcon

557

展示如何在 ASP.NET Web 项目中调用 Web 服务。使用一个已发布的测试 Web 服务;Extentrix Web Services 2.0 Application Edition

Sample Image - maximum width is 600 pixels

引言

Web 服务标志着分布式应用程序开发进入了一个新的简单时代。尽管 Web 服务并非旨在解决所有分布式应用程序问题,但它们是创建和使用 Internet 上服务的简便方法。Web 服务的设计目标之一是允许公司和开发人员以简单的方式通过 Internet 与其他公司共享服务。

Web 服务将 Web 应用程序提升到新的水平。

通过使用 Web 服务,您的应用程序可以将其功能或消息发布给全世界。

Web 服务使用 XML 来编码和解码您的数据,并使用 SOAP 通过开放协议来传输它。

通过 Web 服务,您的会计部门的 Win 2k 服务器账单系统可以与您的 IT 供应商的 UNIX 服务器连接。

使用 Web 服务,您可以交换不同应用程序和不同平台之间的数据。

借助 Microsoft .Net 平台,创建和使用 Web 服务是一项简单的任务。在本文中,我将展示如何在 Web 项目中调用已发布的 Web 服务。

我使用 Extentrix 发布的一个已发布的测试 Web 服务;Extentrix Web Services 2.0 Application Edition (http://www.extentrix.com/webservices/2.0.0/ExtentrixWebServicesForCPS.asmx),该服务旨在帮助开发者社区进行测试和开发。

因此,我将简要解释此 Web 服务 API 的功能。总的来说,Extentrix Web Services for Citrix Presentation Server 帮助您获取特定客户端发布的应用程序信息,并提供指定的详细信息、服务器类型和客户端类型。它还返回ICA文件描述,用于使用给定参数启动应用程序,并检查用户的凭据,如果凭据有效则返回 true。

有关更多信息,请访问 http://www.extentrix.com/Web%20Services/Index.htm

您可以在 http://www.extentrix.com/Web%20Services/Test%20Drive.htm?id=6 找到更多示例、使用并测试此 Web 服务。

背景

首选 ASP.NET 知识

使用代码

消耗 Web 服务的简单步骤

  • 创建网站项目
  • 添加 Web 引用
  • 在代码中调用 Web 服务 API

第一步:创建网站项目

1. 要创建新的网站项目,请从文件菜单中选择新建,然后选择网站,如下图所示。

<formulas></formulas>


2. 选择ASP.NET 网站。命名项目并单击确定

第二步:添加 Web 引用

创建网站项目后,就该为我们的 Web 服务添加 Web 引用了。

1. 在解决方案资源管理器中,右键单击项目节点,选择添加 Web 引用

2. 将会打开一个名为添加 Web 引用的新窗口。

在 URL 字段中,插入 Web 服务的 URL。在本教程中,如我之前提到的,我将使用 Extentrix 的已发布测试 Web 服务。“Extentrix Web Services 2.0 – Application Edition”

http://www.extentrix.com/webservices/2.0.0/ExtentrixWebServicesForCPS.asmx

单击转到按钮后,您将看到 Web 服务 API。

3. 在 Web 引用名称字段中设置您的 Web 服务引用的名称,然后单击添加引用

第三步:在代码中调用 Web 服务 API

成功添加 Web 服务后,现在我们可以开始在我们的项目中调用 Web 服务 API 了。

1. 首先,我们需要将添加的 Web 引用添加到我们的类中。

“ExtentrixWS”是上一步中添加的 Web 服务的名称。

 using ExtentrixWS;  

2. 为我们添加的 Web 服务引用创建一个代理对象,其中 ExtentrixWebServicesForCPS 是 Web 服务的名称。

      //define a web service proxy object.
    private ExtentrixWS.ExtentrixWebServicesForCPS proxy;

3. 正如我之前解释过的,我们需要凭据才能传递给 Citrix Presentation Server,我们将通过 Web 服务 API 传递这些凭据。

     //define a Citrix Presentation Server Credentials object
      private Credentials credentials;

初始化代理和凭据对象。

    //intialaize objects
     proxy = new ExtentrixWebServicesForCPS();
     credentials = new Credentials();

4. 设置 Citrix 凭据的值。我为 Extentrix Web 服务的测试设置了凭据值。

//set credentials
//these values are according to Citrix testdrive presentation server
//for which Extentrix published a web service for delovepers to use it
//as a test web service.
      credentials.Password = "demo";
      credentials.UserName = "citrixdesktop";
      credentials.Domain = "testdrive";
    
   
//because it is a sample,we will use no encryption method.
//so the password will be sent as a clear text.
      credentials.PasswordEncryptionMethod = 0;

//set the domain type to windows domain
      credentials.DomainType = 0;



现在,我们可以像调用任何普通函数一样轻松调用任何可用的 Web 服务。

5. 调用 GetApplicationsByCredentialsEx Web 服务。此 Web 服务接受以下参数:

  • 凭据:用于访问 Citrix Presentation Server Farm 的 Citrix 凭据。
  • 客户端名称:传递您的计算机名称。
  • 客户端 IP:传递您的计算机 IP。
  • 所需详细信息:您请求的详细信息。
  • 服务器类型:传递“all”。
  • 客户端类型:传递“all”。

我将不解释 Extentrix Web 服务 API,如果您有兴趣,可以访问 http://www.extentrix.com/Web%20Services/Index.htm 并查找相关信息。

此 API 返回一个 ApplicationItemEx 数组,该类将在您添加 Web 引用后自动生成。

此类包含已发布的应用程序属性。我使用此 Web 服务获取所有已发布的应用程序,然后为每个应用程序创建了一个 ImageButton。

         // 1) Get all the published applications list by calling GetApplicationsByCredentialsEx web service.
         // 2) create an ImageButton for each application
         // 3) Create Image for the application
         // 4) Add it to the AppList panel.
         // 5) Set the event handler for each ImageButton, so when clicking it the associated application will run
    

       
         //calling the web service
          ApplicationItemEx[] items = proxy.GetApplicationsByCredentialsEx(credentials, Request.UserHostName,
         Request.UserHostAddress, new  string[] { "icon","icon-info"}, new string[]{ "all" },
         new string[] { "all"});
         
    //loop for each published application
    for (int i = 0; i < items.Length; i++) {
           //create the ImageButton
          System.Web.UI.WebControls.ImageButton app = new System.Web.UI.WebControls.ImageButton();


    //set the Image URL to the created image
           app.ImageUrl = createIcon(items[i].InternalName,items[i].Icon);
    
          
    //set the ToolTip to the name of the published application
           app.ToolTip = items[i].InternalName;
    


     //add the ImageButton to the AppList panel
           AppList.Controls.Add(app);
    

           //set the event handler for the ImageButton.
           app.Click += new
    System.Web.UI.ImageClickEventHandler(this.OnApplicationClicked);

            }



最后,另一个调用 Web 服务的示例是启动已发布的应用程序。

在此示例中,在应用程序 ImageButton 的事件处理程序中,我启动了被单击的应用程序。

我通过调用 LaunchApplication Web 服务来获取ICA文件内容。

然后,我将ICA文件内容写入响应以启动应用程序。

    private
        void OnApplicationClicked (object
        sender, System.EventArgs e)
    {     
     ServicePointManager.Expect100Continue = false;
       
        // Get the event source object.     
          System.Web.UI.WebControls.ImageButton app = (System.Web.UI.WebControls.ImageButton)sender;
         
                
        //Get the file ICAfile content by calling LaunchApplication web service.        
        string ica = proxy.LaunchApplication(app.ToolTip, credentials, Request.UserHostName, Request.UserHostAddress);

        
        //Set the response content type to "application/x-ica" to run the file.       
        Response.ContentType = "application/x-ica";
           
        
        //Run the application by writing the file content to the response.
            Response.BinaryWrite(Response.ContentEncoding.GetBytes(ica) );
     Response.End();
    }



参考文献

https://w3schools.org.cn/webservices/default.asp

http://www.extentrix.com/Web%20Services/Index.htm

© . All rights reserved.