码迷,mamicode.com
首页 > 其他好文 > 详细

C# 后台POST和GET 获取数据

时间:2014-06-14 08:49:25      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   tar   

bubuko.com,布布扣
 1 private string PostData(string url, string postData)
 2 {
 3     ASCIIEncoding encoding = new ASCIIEncoding();
 4     byte[] data = encoding.GetBytes(postData);
 5     HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
 6 
 7     myRequest.Method = "POST";
 8     myRequest.ContentType = "application/x-www-form-urlencoded";
 9     myRequest.ContentLength = data.Length;
10     Stream newStream = myRequest.GetRequestStream();
11 
12     newStream.Write(data, 0, data.Length);
13     newStream.Close();
14 
15     HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
16     StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
17     string content = reader.ReadToEnd();
18     reader.Close();
19     return content;
20 }
21 
22 private string GetData(string url)
23 {
24     HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
25     myRequest.Method = "GET";
26     HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
27     StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
28     string content = reader.ReadToEnd();
29     reader.Close();
30     return content;
31 }
View Code

 

C# 后台POST和GET 获取数据,布布扣,bubuko.com

C# 后台POST和GET 获取数据

标签:style   class   blog   code   http   tar   

原文地址:http://www.cnblogs.com/kkwoo/p/3787835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!