C#WinForm POST方式提交给网页(与网页交互) (转)

时间:2016-04-19 09:55:01   收藏:0   阅读:461

提交(POST):

需要导入命名空间:

using System.Net;
using System.IO;

string postData = "username=" + Login_用户名.Text + "&password=" + Login_密码.Text;//POST参数和值写入POSTDATE里
byte[] byteArray = Encoding.Default.GetBytes(postData);
string url = "http://127.0.0.1/user.php"; //POST到网站
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;
Stream newStream = webRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();

 

接收返回信息:

HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.Default);
string Message = php.ReadToEnd();

 

这里的Message即为网页返回的信息!

原文:http://www.cnblogs.com/kunlunmountain/p/5406795.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!