当前位置:编程学习 > XML/UML >>

C#通过POST发送数据函数,类似asp中的xmlhttp

1 //purl处理页面,str参数(如:username=admin&passwod=123456)
 2 
 3 //返回处理页面输出的内容
 4 
 5 //使用:string data = PostData(http://www.soaspx.com/, "action=Fav&str=这个是好网站");
 6 
 7        public static string PostData(string purl,string str)
 8         {
 9             try
10             {
11                 byte[] data = System.Text.Encoding.GetEncoding("GB2312").GetBytes(str);
12                 // 准备请求\ 
13                 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(purl);
14 
15                 //设置超时
16                 req.Timeout = 30000;
17                 req.Method = "Post";
18                 req.ContentType = "application/x-www-form-urlencoded";
19                 req.ContentLength = data.Length;
20                 Stream stream = req.GetRequestStream();
21                 // 发送数据 
22                 stream.Write(data, 0, data.Length);
23                 stream.Close();
24 
25                 HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
26                 Stream receiveStream = rep.GetResponseStream();
27                 Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
28                 // Pipes the str

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,