在Winform中发HTTP请求(调用WebService服务)

2016-01-29 12:43 44 1 收藏

在Winform中发HTTP请求(调用WebService服务),在Winform中发HTTP请求(调用WebService服务)

【 tulaoshi.com - ASP.NET 】


手工发送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")

来源:https://www.tulaoshi.com/n/20160129/1487293.html

延伸阅读
在一次项目开发过程中,用到IOS调用WebService接口,所以抽个空把这方面的内容给大家整理出来,分享给大家。 方法一:使用WSDL2ObjC工具,将接口转成OC类。 1.在浏览器中输入webService接口地址(Safari不可用,我用的是Firefox),如:http://xxx.xxx.asmx, 地址后面添加上.wsdl成http://xxx.xxx.asmx.wsdl打开。 2.将页面另存为wsdl文件...
代码HttpContext类包含了个别HTTP请求的所有特定HTTP信息。这个示例主要是讲如何使用HttpContext类中的User属性来实现用户验证!用户验证是大部分ASP.NET WEB应用程序都要用到的,它在整个应用程序中占有很重要的地位,在.NET中,包含了很多种用户验证方式,如众所周知的PassPort认证,Windows认证,Form认证等等,可是这些都很难满足我们在实...
标签: Web开发
用jQuery调用其他项目的WebService 实现登录验证功能 html输入用户名密码: 代码 代码如下: table tr td Login ID: /td td input type="text" value="" / /td /tr tr td Login Password: /td td input type="password" value="" / /td /tr tr td input value="Sign in" readonly / /td td input value="Sign up" ...
标签: Web开发
网页制作Webjx文章简介:AJAX-向服务器发送一个请求,要想把请求发送到服务器,我们就需要使用open()方法和send()方法。 AJAX-向服务器发送一个请求 要想把请求发送到服务器,我们就需要使用open()方法和send()方法。 open()方法需要三个参数。第一个参数定义发送请求所使用的方法(GET还是POST)。第二个参数规定服...
关于webservice的异步调用简单实例无论在任何情况下,被调用方的代码无论是被异步调用还是同步调用的情况下,被调用方的代码都是一样的, 下面,我们就以异步调用一个webservice 为例作说明。这是一个webservice _ Public Function delCurTable(ByVal tbName As String) As Boolean Try Return True Catch ex As Exception Return False ...

经验教程

47

收藏

62
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部