手动发送HTTP请求调用Web Service
来源:互联网 发布:淘宝清理图片空间 编辑:程序博客网 时间:2024/05/29 17:40
但如果调用Web Service的不是在.NET中,无法直接添加引用怎么办呢?下面就看两个不是直接通过代理类实现对Web Service的调用。(完整代码示例位置:光盘/code/ch07/WinFormsAppClient)
方式一:Get方式的调用
private void button1_Click(object sender, EventArgs e)
{
string strURL = "http://localhost:12074/Service1.asmx/
GetProductPrice?ProductId=";
strURL += this.textBox1.Text;
//创建一个HTTP请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
//request.Method="get";
HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
//转化为XML,自己进行处理
XmlTextReader Reader = new XmlTextReader(s);
Reader.MoveToContent();
string strValue = Reader.ReadInnerXml();
strValue = strValue.Replace("<", "<");
strValue = strValue.Replace(">", ">");
MessageBox.Show(strValue);
Reader.Close();
}
方式二:Post方式的调用
private void button2_Click(object sender, EventArgs e)
{
string strURL = "http://localhost:12074/Service1.asmx/GetProductPrice";
//创建一个HTTP请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
//Post请求方式
request.Method = "POST";
//内容类型
request.ContentType = "application/x-www-form-urlencoded";
//设置参数,并进行URL编码
string paraUrlCoded = HttpUtility.UrlEncode("ProductId");
paraUrlCoded += "=" + HttpUtility.UrlEncode(this.textBox1.Text);
byte[] payload;
//将URL编码后的字符串转化为字节
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
//设置请求的ContentLength
request.ContentLength = payload.Length;
//发送请求,获得请求流
Stream writer = request.GetRequestStream();
//将请求参数写入流
writer.Write(payload, 0, payload.Length);
//关闭请求流
writer.Close();
//获得响应流
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
//转化为XML,自己进行处理
XmlTextReader Reader = new XmlTextReader(s);
Reader.MoveToContent();
string strValue = Reader.ReadInnerXml();
strValue = strValue.Replace("<", "<");
strValue = strValue.Replace(">", ">");
MessageBox.Show(strValue);
Reader.Close();
}
Get请求与Post请求的主要区别在于Post的参数要经过URL编码并在获得请求之前传送,而Get把参数用URL编码后直接附加到请求的URL后面。
URL编码是一种字符编码格式,它确保传递的参数由一致的文本组成(如将空格编码为“%20”)。
由于Web Service返回的是标准的XML文档,所以,基本的原理都是通过WebRequest请求后,自己对返回的XML数据进行处理,得到自己想要的信息。同时,这种方式还有一个好处是可以动态调用不同的WebService接口,比较灵活。
- 手动发送HTTP请求调用Web Service
- 手动发送HTTP请求调用Web Service
- 通过http-Get请求调用Web Service (C#)
- 调用web service 发送手机短信
- Web Service系列之实例之使用http.client发送SOAP POST请求
- SAP 发送 HTTP POST(Web Service)
- 用HTTP GET调用web service
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求、调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http请求,调用http接口与方法
- JAVA发送http get/post请求,调用http接口、方法
- WEB客户端发送给WEB服务器的HTTP请求消息
- Http发送soap消息--Web service最基本形式
- C#通过发送 http 请求调用接口之Get
- 10个让人惊讶的网页设计创意
- GNU C 扩展之__attribute__ 机制简介
- [Win32]一个调试器的实现(十一)显示函数调用栈
- struts2教程:2、第一个struts2应用开发
- STL 随记
- 手动发送HTTP请求调用Web Service
- container_of
- dijkstra算法
- 集体编程智慧(1)
- platform_device和platform_driver
- live555学习2
- 软件破解简单上手 OllyDbg调试器应用
- 选择排序
- 主机PPPOE拨号VMWARE虚拟机在三种模式下上网