小编abd*_*lah的帖子

使用C#发送HTTP POST请求

我尝试使用带有POST的WebRequest发送数据但我的问题是没有数据流传输到服务器.

string user = textBox1.Text;
string password = textBox2.Text;  

ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username" + user + "&password" + password;
byte[] data = encoding.GetBytes(postData);

WebRequest request = WebRequest.Create("http://localhost/s/test3.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();

WebResponse response = request.GetResponse();
stream = response.GetResponseStream();

StreamReader sr99 = new StreamReader(stream);
MessageBox.Show(sr99.ReadToEnd());

sr99.Close();
stream.Close();
Run Code Online (Sandbox Code Playgroud)

这里的结果

c# post httpwebrequest httprequest

7
推荐指数
1
解决办法
4万
查看次数

标签 统计

c# ×1

httprequest ×1

httpwebrequest ×1

post ×1