C# 后台POST和GET 获取数据

 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

 

posted @ 2014-06-14 01:37  平安奕程  阅读(4196)  评论(0编辑  收藏  举报