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(); //返回流
然后从流中读取返回的数据,大概是这样。。。。
微软的参考自己看。
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
展开全部
代码大致是
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 ();
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 ();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询