开发者

Consuming a Web Service purely from an aspx page

My company hosts a SOAP webservice on one of it servers and a requirement has come up to use this webservice to create 'tasks' directly from 开发者_如何学Ca web front-end which I am developing. I have access to the WSDL.

The webservice documentation states that I have to:

1. Call LoginSI, providing the username and password for authentication, receiving the SessionId as a result

2. Execute any number of method calls to the WebService, passing in the SessionId

3. Call LogoutSI, passing in the SessionId. This will abandon the session that was established with LoginSI

This sounds reasonable enough, I have managed to call the LoginSI webservice and retrieve a SessionID using the following simple HTML form:

<form action="http://webservice_server/webservice.asmx/LoginSI" method="post">
    <p>
        Username:<br />
        <input type="text" name="username"/>
    </p>
    <p>
        Password:<br />
        <input type="password" name="password"/>
    </p>

    <input type="submit" value="Test login"/>
</form>

So far so good. The next step is to start executing the other webservices. However what I want is to do all of this from the same page (initially). For example, once you've successfully logged in, the codebehind will handle the webservice response and grab your session ID. Then some other options on the page will appear and then every subsequent call to the webservices will use the session ID.

I have no idea how to consume a webservice purely from codebehind (or in any other way to achieve the desired behaviour). I've found some articles but they mostly mention about adding Assembly references to the project, which I don't think I will be able to do as I didn't write the webservices myself.

Can anyone prod me in the right direction?


I will start the answer from the last point you mention about not being able to reference the webservice's project assembly... You don't have to.

In order to create a strongly typed proxy for that webservice all you need is to use the Add Service Reference option from your web application. The proxy (which is a regular C# class) is a soapclient that will expose all the webservice methods for you to access from the code behind of your aspx page.

The challenge now it to persist the SessionId returned to you from the webservice, for that you have a variety of options, Sessions, ViewState, QueryString... so take your pick.

Of course another possibility is that the webservice was implemented in a RESTful way, meaning there is no wsdl file to allow to create a strongly typed proxy. In which case you can use the WebClient class to manually create a proxy and the service methods, such as:

WebClient proxy = new WebClient();
byte[] data = proxy.DownloadDataAsync("RESTful Method Uri");

Hope this helps..


You can add a reference to a WSDL file through the Add web reference option.

This will generate a proxy that you can use in your code.

See this walk-through on MSDN for details (you can probably just jump to Adding the Web Service as a Component).


You have a couple of options. You can add a web reference for each of the services you need to call, or if that's not possible you might look into calling the web services using the System.Net.WebClient class.

This article explains how to dynamically invoke a web service:

http://blogs.msdn.com/b/kaevans/archive/2006/04/27/dynamically-invoking-a-web-service.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜