当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 在Winform中发HTTP请求(调用WebService服务)

ASP.NET
asp.net GridView控件中模板列CheckBox全选、反选、取消
asp.net GridView 删除时弹出确认对话框(包括内容提示)
asp.net DropDownList 三级联动下拉菜单实现代码
asp DataTable添加列和行的三种方法
Asp.net 页面调用javascript变量的值
asp.net 长文章通过设定的行数分页
asp.net 定时间点执行任务的简易解决办法
asp.net 页面延时五秒,跳转到另外的页面
asp.net 动态输出透明gif图片
asp.net DataList与Repeater用法区别
asp.net Javascript获取CheckBoxList的value
asp.net程序在调式和发布之间图片路径问题的解决方法
asp.net下生成英文字符数字验证码的代码
asp.net 页面版文本框智能提示JSCode (升级版)
ASP.NET URL伪静态重写实现方法
ASP.NET 2.0 中Forms安全认证
asp.net 动态添加多个用户控件
asp.net Repeater显示父子表数据,无闪烁
asp.net 无法获取的内部内容,因为该内容不是文本 的解决方法
asp.net GridView排序简单实现

ASP.NET 中的 在Winform中发HTTP请求(调用WebService服务)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 105 ::
收藏到网摘: n/a


手工发送HTTP请求主要是调用System.Net的HttpWebResponse方法手工发送HTTP的GET请求: string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch?keyword="; strURL +=this.textBox1.Text; System.Net.HttpWebRequest request; //创建一个HTTP请求 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); //request.Method="get"; System.Net.HttpWebResponse response; response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.Stream s; s = response.GetResponseStream(); XmlTextReader Reader = new XmlTextReader(s); Reader.MoveToContent(); string strValue = Reader.ReadInnerXml(); strValue = strValue.Replace("<","< ); strValue = strValue.Replace(">",">"); MessageBox.Show(strValue); Reader.Close();手工发送HTTP的POST请求 string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch"; System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); //Post请求方式 request.Method="POST"; //内容类型 request.ContentType="application/x-www-form-urlencoded"; //参数经过URL编码 string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword"); paraUrlCoded += "=" + System.Web.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(); System.Net.HttpWebResponse response; //获得响应流 response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.Stream s; s = response.GetResponseStream(); 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")