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

 

转载于:https://www.cnblogs.com/kkwoo/p/3787835.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值