C#通过POST发送数据函数,类似asp中的xmlhttp
//purl处理页面,str参数(如:username=admin&passwod=123456)
//返回处理页面输出的内容
//使用:string data = PostData("http://www.zhangxun.net/", "action=Fav&str=这个是好网站");
public static string PostData(string purl,string str)
{
try
{
byte[] data = System.Text.Encoding.GetEncoding("GB2312").GetBytes(str);
// 准备请求...
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(purl);
//设置超时
req.Timeout = 30000;
req.Method = "Post";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
Stream stream = req.GetRequestStream();
// 发送数据
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
Stream receiveStream = rep.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);
Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
StringBuilder sb = new StringBuilder("");
while (count > 0)
{
String readstr = new String(read, 0, count);
sb.Append(readstr);
count = readStream.Read(read, 0, 256);
}
rep.Close();
readStream.Close();
return sb.ToString();
}
catch (Exception ex)
{
return "";
// ForumExceptions.Log(ex);
}
}