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

原创实例:

public string login(MulUsreModel MulUsreItem)
{
//ServiceReferenceTest2.WebServiceTestSoapClient ser = new ServiceReferenceTest2.WebServiceTestSoapClient();
//return ser.login2(MulUsreItem.userModel.UserNameeItem.user,MulUsrModel.PassWord);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://10.116.91.145/mng2/WebService/WebServiceTest.asmx");
httpWebRequest.Method = "POST";
httpWebRequest.Timeout = ;
string param = @"<?xml version=""1.0"" encoding=""utf-8""?>
<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"">
<soap12:Body>
<login2 xmlns=""http://tempuri.org/"">
<username>1</username>
<password>1</password>
</login2>
</soap12:Body>
</soap12:Envelope>";
byte[] bs = Encoding.UTF8.GetBytes(param);
httpWebRequest.ContentType = "application/soap+xml; charset=utf-8";
httpWebRequest.ContentLength = bs.Length;
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(bs, , bs.Length);
}
using (WebResponse myResponse = httpWebRequest.GetResponse())
{
//在这里对接收到的页面内容进行处理
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

XDocument rssXDoc = XDocument.Parse(sr.ReadToEnd());
                  return ((System.Xml.Linq.XElement)rssXDoc.Root.LastNode).Value;

            }
}

通过HttpWebRequest在后台对WebService进行调用的更多相关文章

  1. C# 通过HttpWebRequest在后台对WebService进行调用

    通过HttpWebRequest在后台对WebService进行调用 http://www.cnblogs.com/macroxu-1982/archive/2009/12/23/1630415.ht ...

  2. C# WebService动态调用

    前言 站在开发者的角度,WebService 技术确实是不再“时髦”.甚至很多人会说,我们不再用它.当然,为了使软件可以更简洁,更有层次,更易于实现缓存等机制,我是非常建议将 SOAP 转为 REST ...

  3. WebService服务调用方法介绍

    1 背景概述 由于在项目中需要多次调用webservice服务,本文主要总结了一下java调用WebService常见的6种方式,即:四种框架的五种调用方法以及使用AEAI ESB进行调用的方法. 2 ...

  4. [转贴]C++、C#写的WebService相互调用

    以下宏文(原文在 http://blog.sina.com.cn/s/blog_4e7d38260100ade4.html),是转贴并进行了修饰编辑: 首先感谢永和兄提供C++的WebService服 ...

  5. python通过http请求发送soap报文进行webservice接口调用

    最近学习Python调用webservice 接口,开始的时候主要采用suds 的方式生产client调用,后来发现公司的短信接口采用的是soap报文来调用的,然后开始了谷歌,最后采用httplib ...

  6. 根据wsdl文件,Java工程自动生成webservice客户端调用

    根据wsdl文件,Java工程自动生成webservice客户端调用 1,工具:带有webservice插件的myeclips 2,步骤: (1),新建一个Java工程:relationship (2 ...

  7. 浅谈WebService的调用<转>

    0.前言 前段时间,公司和电信有个合作,产品对接电信的某个平台,使用了WebService接口的调用,实现了业务受理以及单点登录.终于使用到了WebService,楼主还是比较兴奋的,目前功能已经上线 ...

  8. 根据wsdl的url,使用axis1.4生成客户端,并且对webservice进行调用(转)

    根据wsdl的url,使用axis1.4生成客户端,并且对webservice进行调用 axis1.4下载地址 1.到www.apache.org上去下载axis-bin-1_4.zip,如要关联源代 ...

  9. 浅谈WebService的调用

    0.前言 前段时间,公司和电信有个合作,产品对接电信的某个平台,使用了WebService接口的调用,实现了业务受理以及单点登录.终于使用到了WebService,楼主还是比较兴奋的,目前功能已经上线 ...

随机推荐

  1. 转:sock_ev——linux平台socket事件框架(uri地址的解析) .

    在第一篇中,已经说明,传递的socket地址采取以下形式: [cpp] view plaincopyprint?stream://192.168.2.10:8080   dgram://192.168 ...

  2. 【微信支付】公众号、商户基础配置和流程(包括设置支付授权目录、测试支付目录和白名单、JS接口安全域名、授权回调域名等)

    一.使用场景以及说明 使用场景:商户已有H5商城网站,用户通过消息或扫描二维码在微信内打开网页时,可以调用微信支付完成下单购买的流程. 说明:1.用户打开图文消息或者扫描二维码,在微信内置浏览器打开网 ...

  3. 使用 Python 的 matplotlib 绘图库进行绘图

    matplotlib 是 Python 最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 1  使用 Ma ...

  4. Linux系统里如何彻底的清空终端屏幕?

    Linux用户,特别是Ubuntu或CentOS用户,基本上都习惯使用clear命令或Ctrl+L组合快捷键来清空终端屏幕.但是,这样做其实并不是真正的清空屏幕,它只是给人一种错觉,让人以为屏幕清空了 ...

  5. bcdedit的研究

    首先说明下引导: 微软在Vista之前的系统,采用的是Ntldr来进行引导系统,使用的是boot.ini文件. 在目前的Vista和win7中,采用的是新的引导方式Windows Boot Manag ...

  6. C语言的存储类型和关键字extern、static

    1.C语言中每个变量都有3个性质:存储期限.作用域.链接 1)存储期限:变量的存储期限决定了为变量预留的内存被释放的时间.共2种,自动存储期限(auto),静态存储期限(static),自动存储(au ...

  7. SignalTap ii的使用

    1.实现原理 SignalTap II获取实时数据的原理是在工程中引入Megafunction中的ELA(Embedded Logic Analyzer),以预先设定的时钟采样实时数据,并存储于FPG ...

  8. Eclipse中导入JDK类库的源代码以及添加指定的API

    一.在Eclipse中导入JDK类库的源代码 操作步骤: 打开eclipse->“window”-> “Preferences” -> “Java” -> “Installed ...

  9. Java 之进制转换

    //十进制转十六进制 import java.util.Scanner; public class Main{ public static void main(String[] args){ Scan ...

  10. poj3261(后缀数组)

    题意:给出一串长度为n的字符,再给出一个k值,要你求重复次数大于等于k次的最长子串长度........ 思路:其实也非常简单,直接求出height值,然后将它分组,二分答案......结果就出来了.. ...