通过HttpWebRequest在后台对WebService进行调用
来源:互联网 发布:ssd优化设置 编辑:程序博客网 时间:2024/05/03 05:54
目录:
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);
}
- 通过HttpWebRequest在后台对WebService进行调用
- 通过HttpWebRequest在后台对WebService进行调用
- 使用HttpWebRequest和HttpWebResponse在后台进行文件的请求和响应
- 在后台通过JAVASCRIPT代码调用按钮的事件
- C#通过构造Soap信息使用HttpWebRequest调用WebService
- 【Hibernate】把Hibernate在后台查询到的数据通过Struts标签在前台进行输出
- ORMLite在后台service中进行数据处理
- 使用UpdatePanel,在后台调用alert
- 在后台调用FormView控件并赋值
- 在后台调用javascript中的function
- wpf 根据DataTable在后台自动创建DataGrid,而且可以对数据进行初步处理,显示差异
- 客户端利用CXF中的JaxWsProxyFactoryBean 对webservice地址进行调用
- 如何通过Linux bash在后台运行任务
- 通过service和handler实现Notification在后台打开activity
- asp.net中在后台通过js弹出新窗口
- Ajax中通过response在后台传递数据
- Android之通过ActivityLifecycleCallbacks判断程序是否运行在后台
- 通过UIBackgroundTaskIdentifier在后台挂起时依旧能执行代码
- x & (x - 1)式子的含义
- 简单使用Boost线程池threadpool
- maven profile介绍
- 使用spring测试模块搭建自动测试平台
- VC中CTreeCtrl控件单击事件,如何判断是单击的是某项的按钮,还是单击的文字
- 通过HttpWebRequest在后台对WebService进行调用
- 1.1算法入门之百钱白鸡问题
- javascript的时间事件
- JAVA打印二维码图片自定义高度
- gdb调试(查看函数栈、调试coredump文件)
- Smarty学习笔记——变量调节器
- 类似于微信发语音Demo
- iOS 如何用oc代码在苹果沙盒里面创建一个html界面
- tomcat访问配置