C# winform http协议 如何向服务器端发送消息?使用post 给个示例 ,谢谢!

如题所述

byte[] byteArray=System.Text.Encoding.Default.GetByte("要发送的post数据");//编码Post数据,编码根据自己需求定,Default为默认编码
WebRequest request=HttpWebRequest.Create //创建请求
request.Method = "POST";
request.ContentLength = byteArray.Length;
request.ContentType = "application/x-www-form-urlencoded";
Stream dataStream = request.GetRequestStream (); //创建输入流
dataStream.Write (byteArray, 0, byteArray.Length);//写Post数据
dataStream.Close();//关闭流

WebResponse response=request.GetResponse()// 读取网站返回
Stream responseStream =response.GetResponseStream(); //返回流
然后从流中读取返回的数据,大概是这样。。。。
微软的参考自己看。

参考资料:http://msdn.microsoft.com/zh-cn/library/debx8sh9%28VS.80%29.aspx

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-03
代码大致是

WebRequest req = WebRequest.Create(URI);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte [] bytes = System.Text.Encoding.ASCII.GetBytes(data);
req.ContentLength = bytes.Length;
Stream os = req.GetRequestStream ();
os.Write (bytes, 0, bytes.Length); //Push it out there
os.Close ();

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网