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

2024-11-21 21:09:10
推荐回答(2个)
回答1:

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(); //返回流
然后从流中读取返回的数据,大概是这样。。。。
微软的参考自己看。

回答2:

代码大致是

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 ();