在客户端调用Web服务 [英] calling Web Services on client side

查看:81
本文介绍了在客户端调用Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用.NET代理类在客户端调用Web服务吗????

Can we call Web services on the client side using .NET proxy classes????

推荐答案

使用Ajax或JavaScript调用Webservice的步骤:

步骤1:创建XMLHttpRequest对象.下面给出的方法根据浏览器返回正确的XMLHttpRequest对象:

Steps to call webservice using Ajax or JavaScript:

Step 1: Create XMLHttpRequest object. Given below method returns proper XMLHttpRequest object depending upon browser:

function CreateXMLHttpRequest() {
        if (typeof XMLHttpRequest != "undefined"){
            //All modern browsers (IE7+, Firefox, Chrome, Safari, and Opera) uses XMLHttpRequest object
            return new XMLHttpRequest();
     }
        else if (typeof ActiveXObject != "undefined") {
            // Internet Explorer (IE5 and IE6) uses an ActiveX object
            return new ActiveXObject("Microsoft.XMLHTTP");
     }
        else {
            throw new Error("XMLHttpRequestnot supported");
     }
    }



第2步:调用open方法并根据要求传递适当的参数



Step 2: Call open method and pass proper parameter as per the requirement

var objXMLHttpRequest = CreateXMLHttpRequest(); //Create XMLHttpRequest object
objXMLHttpRequest.open("POST", <serviceUrl>, false);


打开方法接受三个参数,如下所述:


Open method accept three parameters, They are mentioned below:

method - Type of request, it can be GET or POST.
serviceUrl - Url of the webservice or page URL.
isAsync - It will be either 'true' or 'false'. Pass true to make asynchronus webservice call.


// Add mentioned below code if your application calls webservice Asynchronously.
objXMLHttpRequest.onreadystatechange = function (){
       if (objXMLHttpRequest.readyState ==4 && objXMLHttpRequest.status == 200){
             result = objXMLHttpRequest.responseXML;
       }
}

步骤3:使用"setRequestHeader"方法设置Http请求的标头.例如,假设我们必须传递XML数据以调用Webservice.使用下面提到的代码:

Step 3: Use "setRequestHeader" method to set header of the Http request. For example suppose we have to pass XML data to call webservice. Use mentioned below code:

objXMLHttpRequest.setRequestHeader("Content-Type","text/xml; charset=utf-8");

注意:假设您不必在send方法中传递任何东西,那么就不需要设置content-type.

步骤4:最后,我们必须准备数据包并调用send方法.创建数据包时请务必小心,因为您犯的小错误可能会浪费大量时间进行调试.

Note: Suppose you do not have to pass any thing in send method then no need set content-type.

Step 4: Finally we have to prepare packet and call send method. Be careful while creating packet because you make small mistake that can waste a lots of time in debugging.

var packet = '<?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>

   <WebMethodName xmlns="<Webservice Namespace>">

  <ParameterName>value</ParameterName>

  <ParameterName>value</ParameterName >

  <ParameterName>value</ParameterName>

  </WebMethodName>

     </soap:Body>  </soap:Envelope>';



在发送xml http请求之前,我们需要在上述数据包中用适当的值替换突出显示的文本.目的突出显示的文本如下:



We need to replace highlighted text with the proper value in the above packet before sending the xml http request. Purpose highlighted text are mentioned below:

WebMethodName - Webmethod name and method name is case sensitive.
<Webservice Namespace> - Namespace of webservice "http://SampleWebservice.com/"
e.g:[WebService(Namespace = "http://SampleWebservice.com/")]
ParameterName - Parameter Name of webmethod and its value
        objXMLHttpRequest.send(packet);


// Add mentioned below code if your application calls webservice synchronously.
       result = objXMLHttpRequest.responseXML;


现在,我们知道了如何使用Ajax或JavaScript调用Web服务.下面提到了使用Ajax进行简单Web服务和Web方法调用的示例代码.

示例Web服务代码:


Now we know how to call webservice using Ajax or JavaScript. Mentioned below sample code of a simple webservice and web-method call using Ajax.

Sample webservice code:

/// <summary>
/// Summary description for SampelWebservice
/// </summary>
[WebService(Namespace= "http://SampleWebservice.com/")]
[WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class SampelWebservice: System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld(string text)
    {
        return text;
    }
}



Web服务调用的示例代码:



Sample Code for webservice call:

var objXMLHttpRequest = CreateXMLHttpRequest();

objXMLHttpRequest.open("POST", "http://localhost/SampleWebservice.asmx", true);

objXMLHttpRequest.setRequestHeader("SOAPAction","http://SampleWebservice.com/HelloWorld");

var packet = '<?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>

   <HelloWorld xmlns="http://SampleWebservice.com/">

  <text> Asynchronous webservice call using Ajax</text>

  </WebMethodName>

     </soap:Body>

     </soap:Envelope>';



objXMLHttpRequest.onreadystatechange = function(){

       if (objXMLHttpRequest.readyState == 4 && objXMLHttpRequest.status == 200) {

            result= objXMLHttpRequest.resposeXML;

        }

}

objXMLHttpRequest.send(packet);



希望它能帮助您了解Ajax的使用.



I hope, it will help you understand use of Ajax.


这篇关于在客户端调用Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆