首页 > 代码库 > C# 后台POST和GET 获取数据
C# 后台POST和GET 获取数据
1 private string PostData(string url, string postData) 2 { 3 ASCIIEncoding encoding = new ASCIIEncoding(); 4 byte[] data =http://www.mamicode.com/ 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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。