最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

在后台对WebService进行调用

日期:09-26    来源:中国设计秀    作者:cnwebshow.com

  通过HttpWebRequest在后台对WebService进行调用1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
目录:1KP中国设计秀
1KP中国设计秀
1  后台调用Webservice的业务需求1KP中国设计秀
1KP中国设计秀
2  WebService支持的交互协议1KP中国设计秀
1KP中国设计秀
3  如何配置WebService支持的协议1KP中国设计秀
1KP中国设计秀
4  后台对WebService的调用1KP中国设计秀
1KP中国设计秀
4.1 SOAP 1.1 后台调用实例1KP中国设计秀
1KP中国设计秀
4.2 SOAP 1.2 后台调用实例1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
注:本文章的开发环境为VSS2008  .net FrameWork 3.51KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
本文章设计到使用的代码示例的WebService 为1KP中国设计秀
1KP中国设计秀
服务路径:http://localhost/WebServiceTest/Service1.asmx1KP中国设计秀
1KP中国设计秀
服务接口:1KP中国设计秀
1KP中国设计秀
[WebMethod]1KP中国设计秀
1KP中国设计秀
        public string HelloWorld(string StudentName,string Password)1KP中国设计秀
1KP中国设计秀
        {1KP中国设计秀
1KP中国设计秀
            return "Hello World";1KP中国设计秀
1KP中国设计秀
        }1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1  后台调用Webservice的业务需求1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
   在实际开发环境中,我们常常调用WebService时,通过项目中引用现实部署的WebService的Asmx文件,生成客户端代理类的方式。这种方式将和WebService进行了二次封装,并以代理类的方式进行调用,有利用简单,快捷的开发。1KP中国设计秀
1KP中国设计秀
这种开发方式包含了两个重要的问题1KP中国设计秀
1KP中国设计秀
1) 在开发环境中必须可以访问需要调用的WebService,在开发一些大公司的内网系统时,我们往往在开发环境中访问不到,只仅仅在部署环境中访问。1KP中国设计秀
1KP中国设计秀
2)WebService的接口发生版本变更,我们的应用系统需要重新编译并部署。1KP中国设计秀
1KP中国设计秀
    1KP中国设计秀
1KP中国设计秀
在发现以上的困惑后,直觉告诉我们,我们需要一种直接通过交互协议的方式进行访问WebService。就像网页爬虫一样,去交互业务操作。1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
2  WebService支持的交互协议1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
  1KP中国设计秀
1KP中国设计秀
WebService支持 三种方式1KP中国设计秀
1KP中国设计秀
1)Http post 方式(注意这种方式只对于本机调试使用,在web服务部署在其他机器上,应用程序不能通过 Http Post方式调用)1KP中国设计秀
1KP中国设计秀
       具体交互格式如下:1KP中国设计秀
1KP中国设计秀
POST /WebServiceTest/Service1.asmx/HelloWorld HTTP/1.11KP中国设计秀
1KP中国设计秀
Host: localhost1KP中国设计秀
1KP中国设计秀
Content-Type: application/x-www-form-urlencoded1KP中国设计秀
1KP中国设计秀
Content-Length: length1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
StudentName=string&Password=string1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
2)SOAP1.1协议  注意Soap协议是基于HTTP的协议,也就是在HTTP的基础上再次封装1KP中国设计秀
1KP中国设计秀
交互格式如下:1KP中国设计秀
1KP中国设计秀
POST /WebServiceTest/Service1.asmx HTTP/1.11KP中国设计秀
1KP中国设计秀
Host: localhost1KP中国设计秀
1KP中国设计秀
Content-Type: text/xml; charset=utf-81KP中国设计秀
1KP中国设计秀
Content-Length: length1KP中国设计秀
1KP中国设计秀
SOAPAction: "http://tempuri.org/HelloWorld"1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
<?xml version="1.0" encoding="utf-8"?>1KP中国设计秀
1KP中国设计秀
<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/">1KP中国设计秀
1KP中国设计秀
  <soap:Body>1KP中国设计秀
1KP中国设计秀
    <HelloWorld xmlns="http://tempuri.org/">1KP中国设计秀
1KP中国设计秀
      <StudentName>string</StudentName>1KP中国设计秀
1KP中国设计秀
      <Password>string</Password>1KP中国设计秀
1KP中国设计秀
    </HelloWorld>1KP中国设计秀
1KP中国设计秀
  </soap:Body>1KP中国设计秀
1KP中国设计秀
</soap:Envelope>1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
3)SOAP1.2 协议1KP中国设计秀
1KP中国设计秀
交互格式如下:1KP中国设计秀
1KP中国设计秀
POST /WebServiceTest/Service1.asmx HTTP/1.11KP中国设计秀
1KP中国设计秀
Host: localhost1KP中国设计秀
1KP中国设计秀
Content-Type: application/soap+xml; charset=utf-81KP中国设计秀
1KP中国设计秀
Content-Length: length1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
<?xml version="1.0" encoding="utf-8"?>1KP中国设计秀
1KP中国设计秀
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">1KP中国设计秀
1KP中国设计秀
  <soap12:Body>1KP中国设计秀
1KP中国设计秀
    <HelloWorld xmlns="http://tempuri.org/">1KP中国设计秀
1KP中国设计秀
      <StudentName>string</StudentName>1KP中国设计秀
1KP中国设计秀
      <Password>string</Password>1KP中国设计秀
1KP中国设计秀
    </HelloWorld>1KP中国设计秀
1KP中国设计秀
  </soap12:Body>1KP中国设计秀
1KP中国设计秀
</soap12:Envelope>1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
3  如何配置WebService支持的协议1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
WebService支持的协议 包含两种 Soap1.1 Soap1.2 对于webService 来讲可以通过配置文件配置,支持那些协议,默认的情况下 两种协议都支持。1KP中国设计秀
1KP中国设计秀
具体的配置方式为:1KP中国设计秀
1KP中国设计秀
在配置文件中1KP中国设计秀
1KP中国设计秀
<webServices>1KP中国设计秀
1KP中国设计秀
  <PRotocols>1KP中国设计秀
1KP中国设计秀
    <add name="HttpSoap1.2"/>1KP中国设计秀
1KP中国设计秀
    <add name="HttpSoap1.1"/>1KP中国设计秀
1KP中国设计秀
</protocols>1KP中国设计秀
1KP中国设计秀
</webServices>1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
4  后台对WebService的调用1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
4.1 SOAP 1.1 后台调用实例1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
                  string   str1=""双引号"";   1KP中国设计秀
1KP中国设计秀
                Console.WriteLine("新开始进行连接测试");1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
                string param = @"<?xml version=""1.0"" encoding=""utf-8""?>1KP中国设计秀
1KP中国设计秀
                                        <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/"">1KP中国设计秀
1KP中国设计秀
                                          <soap:Body>1KP中国设计秀
1KP中国设计秀
                                            <HelloWorld xmlns=""http://tempuri.org/"">1KP中国设计秀
1KP中国设计秀
                                              <StudentName>1</StudentName>1KP中国设计秀
1KP中国设计秀
                                              <Password>1</Password>1KP中国设计秀
1KP中国设计秀
                                            </HelloWorld>1KP中国设计秀
1KP中国设计秀
                                          </soap:Body>1KP中国设计秀
1KP中国设计秀
                                        </soap:Envelope>";1KP中国设计秀
1KP中国设计秀
                byte[] bs = Encoding.UTF8.GetBytes(param);1KP中国设计秀
1KP中国设计秀
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://fox-gaolijun/Short_Message/Service1.asmx");1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
                myRequest.Method = "POST";1KP中国设计秀
1KP中国设计秀
                myRequest.ContentType = "text/xml; charset=utf-8";1KP中国设计秀
1KP中国设计秀
                myRequest.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld");1KP中国设计秀
1KP中国设计秀
                myRequest.ContentLength = bs.Length;1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
                Console.WriteLine("完成准备工作");1KP中国设计秀
1KP中国设计秀
                using (Stream reqStream = myRequest.GetRequestStream())1KP中国设计秀
1KP中国设计秀
                {1KP中国设计秀
1KP中国设计秀
                    reqStream.Write(bs, 0, bs.Length);1KP中国设计秀
1KP中国设计秀
                }1KP中国设计秀
1KP中国设计秀
                1KP中国设计秀
1KP中国设计秀
                using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())1KP中国设计秀
1KP中国设计秀
                {1KP中国设计秀
1KP中国设计秀
                    StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);1KP中国设计秀
1KP中国设计秀
                    responseString = sr.ReadToEnd();1KP中国设计秀
1KP中国设计秀
                    Console.WriteLine("反馈结果" + responseString);1KP中国设计秀
1KP中国设计秀
                }   1KP中国设计秀
1KP中国设计秀
                Console.WriteLine("完成调用接口");1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
            }1KP中国设计秀
1KP中国设计秀
            catch (Exception e)1KP中国设计秀
1KP中国设计秀
            {1KP中国设计秀
1KP中国设计秀
                Console.WriteLine(System.DateTime.Now.ToShortTimeString() + "LBS EXCEPTION:" + e.Message);1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
                Console.WriteLine(e.StackTrace);1KP中国设计秀
1KP中国设计秀
            }1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
4.1 SOAP 1.2 后台调用实例1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
Console.WriteLine("新开始进行连接测试");1KP中国设计秀
1KP中国设计秀
                string responseString;1KP中国设计秀
1KP中国设计秀
                string param = @"<?xml version=""1.0"" encoding=""utf-8""?>1KP中国设计秀
1KP中国设计秀
                                <soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">1KP中国设计秀
1KP中国设计秀
                                 <soap12:Body>1KP中国设计秀
1KP中国设计秀
                                    <HelloWorld xmlns=""http://tempuri.org/"">1KP中国设计秀
1KP中国设计秀
                                        <StudentName>1212</StudentName>1KP中国设计秀
1KP中国设计秀
                                         <Password>12121</Password>1KP中国设计秀
1KP中国设计秀
                                    </HelloWorld>1KP中国设计秀
1KP中国设计秀
                                </soap12:Body>1KP中国设计秀
1KP中国设计秀
                            </soap12:Envelope>";1KP中国设计秀
1KP中国设计秀
                byte[] bs = Encoding.UTF8.GetBytes(param);1KP中国设计秀
1KP中国设计秀
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("  http://fox-gaolijun/Short_Message/Service1.asmx");1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
                myRequest.Method = "POST";1KP中国设计秀
1KP中国设计秀
                myRequest.ContentType = "application/soap+xml; charset=utf-8";1KP中国设计秀
1KP中国设计秀
                myRequest.ContentLength = bs.Length;1KP中国设计秀
1KP中国设计秀
             1KP中国设计秀
1KP中国设计秀
                Console.WriteLine("完成准备工作");1KP中国设计秀
1KP中国设计秀
                using (Stream reqStream = myRequest.GetRequestStream())1KP中国设计秀
1KP中国设计秀
                {1KP中国设计秀
1KP中国设计秀
                    reqStream.Write(bs, 0, bs.Length);1KP中国设计秀
1KP中国设计秀
                }1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
                using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())1KP中国设计秀
1KP中国设计秀
                {1KP中国设计秀
1KP中国设计秀
                    StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);1KP中国设计秀
1KP中国设计秀
                    responseString = sr.ReadToEnd();1KP中国设计秀
1KP中国设计秀
                    Console.WriteLine("反馈结果" + responseString);1KP中国设计秀
1KP中国设计秀
                }1KP中国设计秀
1KP中国设计秀
                Console.WriteLine("完成调用接口");1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
            }1KP中国设计秀
1KP中国设计秀
            catch (Exception e)1KP中国设计秀
1KP中国设计秀
            {1KP中国设计秀
1KP中国设计秀
                Console.WriteLine(System.DateTime.Now.ToShortTimeString() + "LBS EXCEPTION:" + e.Message);1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
1KP中国设计秀
                Console.WriteLine(e.StackTrace);1KP中国设计秀
1KP中国设计秀
            }1KP中国设计秀
1KP中国设计秀
 1KP中国设计秀

本文引用地址:/bc/article_46200.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明