Click here to Skip to main content
15,861,172 members
Articles / Web Development / ASP.NET
Article

Calling Web Service using ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.71/5 (19 votes)
7 Jan 2008CPOL4 min read 506K   6.2K   60   11
Shows how to call a Web service inside ASP.NET Web project using a test published Web service: Extentrix Web Services 2.0 Application Edition
Image 1

Introduction

Web services signal a new age of trivial distributed application development. While Web services are not intended nor do they have the power to solve every distributed application problem, they are an easy way to create and consume services over the Internet. One of the design goals for Web Services is to allow companies and developers to share services with other companies in a simple way over the Internet.
Web services take Web applications to the next level.
Using Web services, your application can publish its function or message to the rest of the world.

Web services use XML to code and decode your data and SOAP to transport it using open protocols.

With Web services, your accounting departments Win 2K servers' billing system can connect with your IT suppliers UNIX server.

Using Web services, you can exchange data between different applications and different platforms.

With Microsoft .NET platform, it is a simple task to create and consume Web Services. In this article, I am going to show how to call a published Web service inside a Web project.
I use a test published Web service; Extentrix Web Services 2.0 Application Edition that Extentrix published for the developer community to help them in testing and developing.

So I'll simply explain the functions of this Web services APIs. In general Extentrix Web Services for Citrix Presentation Server helps you get information about a published application for a specific client with the specified details, server types, and client types. It also returns the ICAfile description to be used to launch an application with a given parameter and checks the user's credentials and returns true if they are valid.

For more information, visit this website.
You can find more samples, use this web service, and test it here.

Background

Knowledge in ASP.NET is preferred.

Using the Code

Simple Steps to Consume a Web Service

  1. Create a Web Site project
  2. Add a Web Reference
  3. Call the Web services APIs inside the code

First Step: Create a Web Site Project

  1. To create a new Web Site project, choose New from File menu, then choose Web Site as shown below:

    Image 2
  2. Choose ASP.NET Web Site. Name the project and click OK:

    Image 3

Second Step: Add a Web Reference

After creating the Web Site project, it’s time to add a Web reference for our Web service.

  1. In the solution explorer, right click the project node, choose Add Web Reference:

    Image 4
  2. A new window with Add Web Reference title will be opened:

    Image 5

    In the URL field, insert the URL for the Web service. In this tutorial, as I mentioned before, I'll use the test published Web services from Extentrix: “Extentrix Web Services 2.0 – Application Edition”.

    After clicking the Go button, you will see the Web services APIs.

  3. Set a name for your Web service reference in the Web reference name field and click Add Reference:

    Image 6

Third Step: Call the Web Services APIs Inside the Code

After successfully adding to the Web service, now we are ready to call the Web services APIs inside our project.

  1. First we need to add the added Web reference to our class.
    ExtentrixWS is the name of the added Web service from the previous step.

    C#
    using ExtentrixWS;  
  2. Create a proxy object for our added Web service reference, where ExtentrixWebServicesForCPS is the name of the Web Services.

    C#
    //define a Web service proxy object.
    private ExtentrixWS.ExtentrixWebServicesForCPS proxy;
  3. As I explained before, we need credentials to pass to the Citrix Presentation Server. We will pass these credentials through the Web services APIs:

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

    Initialize the proxy and the credentials objects:

    C#
    //initialize objects
    proxy = new ExtentrixWebServicesForCPS();
    credentials = new Credentials();
  4. Set the values for Citrix credentials. I set the credentials values for the test of Extentrix Web Service:

    C#
    //set credentials
    //these values are according to Citrix testdrive presentation server
    //for which Extentrix published a web service for developers 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;

    Now we can call any Web services available. It is as simple as calling any ordinary function.

  5. Call the GetApplicationsByCredentialsEx Web service. This web service takes the following parameters:
    • Credentials: Citrix Credential to access Citrix Presentation Server Farm
    • Client Name: Pass your machine name
    • Client IP: Pass your machine IP
    • Desired Details : Details you asked for
    • Server Types: Pass “all”
    • Client Types: Pass “all”

I am not going to explain Extentrix Web services APIs, if you are interested, you can go here and look for it.

This API returns an array of ApplicationItemEx. This class will be built for you once you add the Web reference.

This class contains the published application properties. I used this Web service to get all the published applications, and then I created an ImageButton for each application.

C#
// 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);
}

Finally, another example in calling a Web service is to launch the published application.
In this example, in the event handler of the applications ImageButtons I launch the clicked application.

I get the ICA file content by calling LaunchApplication Web service. Then I write the ICA file content to the response to launch the application.

C#
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 = 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();
}

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Extentrix Systems
Jordan Jordan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Question"Error downloading" When attempting at add Web Reference Pin
corvanica28-Oct-14 7:20
corvanica28-Oct-14 7:20 
QuestionOP got an error in your article Pin
Prasad_Kulkarni28-Aug-12 2:43
Prasad_Kulkarni28-Aug-12 2:43 
QuestionAuthentication Mode error -- Add web Reference Pin
msbyuva29-Jun-12 4:03
msbyuva29-Jun-12 4:03 
GeneralMy vote of 1 Pin
mhamad zarif15-Nov-11 10:11
mhamad zarif15-Nov-11 10:11 
GeneralRuntime Error Pin
rod cortejo19-Jun-10 16:48
rod cortejo19-Jun-10 16:48 
GeneralFirst Step Pin
Joe Torres9-Feb-10 13:29
Joe Torres9-Feb-10 13:29 
GeneralMy vote of 1 Pin
yrbyogi7-Aug-09 23:30
yrbyogi7-Aug-09 23:30 
GeneralNot Showing Added Webservice Pin
Narendra Reddy Vajrala12-Mar-09 4:20
Narendra Reddy Vajrala12-Mar-09 4:20 
GeneralRe: Not Showing Added Webservice Pin
suren.info11-Jun-09 2:45
suren.info11-Jun-09 2:45 
QuestionThe XML page cannot be displayed Pin
beajow13-Nov-08 13:15
beajow13-Nov-08 13:15 
QuestionError adding a wev reference Pin
claudio.sanchez31-Jan-08 9:09
claudio.sanchez31-Jan-08 9:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.