Truly
2005-08-17

  • 下载本文的源码(C#) - 25k
  • 下载本文的源码(VB) - 25k

     

    摘要

    很多时候大家需要使用xmlhttp来实现无刷新的从服务器获取数据,本文以实例代码来介绍本在客户端对WebService的调用

    主要分为3种类型

  • 通过SOAP对WebService进行调用
  • 通过HTTP POST对WebService进行调用
  • 借助MS的webservice.htc简化调用

    注:本文代码均以C#代码作为基础,对于使用VB代码的需要注意WebService的命名空间,详细请看下载链接中源码

    SOAP对WebService进行调用

                            
                            
    JavaScript vbscript  

    通过HTTP POST对WebService进行调用

                            
                            
    JavaScript vbscript  

    借助MS的webservice.htc简化调用

    webservice.htc是MS提供的一个对webservie常用方法的封装,可以从ms官方网站下载,安装iewebcontrols时也会安装到你的网站根目录下,使用时需要注意路径。请注意代码中body的属性设置,更多对WebService.htc的介绍,点击这里

     

                            
     

     

    Web Service 的完整代码(C#, Service1.asmx.cs)

    using System;
                            using System.Collections;
                            using System.ComponentModel;
                            using System.Data;
                            using System.Diagnostics;
                            using System.Web;
                            using System.Web.Services;
                            namespace WSInvoke
                            {
                            /// <SUMMARY>
                            /// Summary description for Service1.
                            /// </SUMMARY>
                            public class Service1 : System.Web.Services.WebService
                            {
                            public Service1()
                            {
                            //CODEGEN: This call is required by the ASP.NET Web Services Designer
                            InitializeComponent();
                            }
                            #region Component Designer generated code
                            //Required by the Web Services Designer
                            private IContainer components = null;
                            /// <SUMMARY>
                            /// Required method for Designer support - do not modify
                            /// the contents of this method with the code editor.
                            /// </SUMMARY>
                            private void InitializeComponent()
                            {
                            }
                            /// <SUMMARY>
                            /// Clean up any resources being used.
                            /// </SUMMARY>
                            protected override void Dispose( bool disposing )
                            {
                            if(disposing && components != null)
                            {
                            components.Dispose();
                            }
                            base.Dispose(disposing);
                            }
                            #endregion
                            // WEB SERVICE EXAMPLE
                            // The HelloWorld() example service returns the string Hello World
                            // To build, uncomment the following lines then save and build the project
                            // To test this web service, press F5
                            [WebMethod]
                            public string HelloWorld(int i)
                            {
                            return "Hello World" + i;
                            }
                            }
                            }
                            
     

     

    测试所用的html页面(Invoke.html)

    <HTML>
                            <BODY>
                            <INPUT type="button" value="SOAP" onclick="GetHelloWorld_SOAP('1')" >
                            <INPUT type="button" value="HTTPPOST" onclick="GetHelloWorld_HTTPPOST('2')" >
                            <INPUT type="button" value="异常测试" onclick="GetHelloWorld_SOAP('')" ><BR><BR>
                            <INPUT type="button" value="vbSOAP" onclick="vbGetHelloWorld_SOAP('3')" >
                            <INPUT type="button" value="vbHTTPPOST" onclick="vbGetHelloWorld_HTTPPOST('4')" >
                            </BODY>
                            </HTML>
                            
     
    很晚了,先把代码贴上。有空的时候把异步操作再仔细分析一下 :B

     

    附表1(IXMLHTTPRequest - 属性)

    Name Type Description
    onreadystatechange N/A 指定当就绪状态发生改变时调用的事件处理函数,仅用于异步操作
    readyState Long 异步操作的状态:未初始化(0),正在加载(1),已加载(2),交互(3),已完成(4)
    responseBody Variant 将响应信息正文作为unsigned byte数组返回
    responseStream Variant 将响应信息正文作为一个ADO Stream对象返回
    responseText String 将响应信息正文作为一个文本字符串返回
    responseXML Object 通过XMLDom将响应信息正文解析为XMLDocument对象
    status Long 服务器返回的HTTP状态码
    statusText String 服务器HTTP响应行状态

    附表2(IXMLHTTPRequest - 方法)

    Name Desciption
    abort 取消当前 HTTP 请求
    getAllResponseHeaders 从响应信息中检索所有的标头字段
    getResponseHeader 从响应信息正文中获得一个 HTTP 标头值
    open(method, url, boolAsync, bstrUser, bstrPassword) 打开一个与 HTTP 服务器的连接
    send(varBody) 设定一个请求的标头字段
    setRequestHeader(bstrHeader, bstrValue) 向 HTTP 服务器发送请求。可包含正文。
  • 相关文章: