码迷,mamicode.com
首页 > Web开发 > 详细

.Net 请求Web接口Post和Get方法

时间:2017-07-12 12:03:53      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:获取   encoding   method   val   creat   obj   net   eva   text   

#region web服务请求 get post
static string DefaultUserAgent = "www.zhiweiworld.com";
public static String Get(string url)
{
System.Net.HttpWebRequest request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
request.Method = "GET";
request.UserAgent = DefaultUserAgent;
System.Net.HttpWebResponse result = request.GetResponse() as System.Net.HttpWebResponse;
System.IO.StreamReader sr = new System.IO.StreamReader(result.GetResponseStream(), Encoding.UTF8);
string strResult = sr.ReadToEnd();
sr.Close();
//Console.WriteLine(strResult);
return strResult;
}

public static String Post(string url, System.Collections.Specialized.NameValueCollection para)
{
System.Net.WebClient WebClientObj = new System.Net.WebClient();

byte[] byRemoteInfo = WebClientObj.UploadValues(url, "POST", para);//请求地址,传参方式,参数集合

string rtContent = System.Text.Encoding.UTF8.GetString(byRemoteInfo);//获取返回值 
return rtContent;
}
#endregion

.Net 请求Web接口Post和Get方法

标签:获取   encoding   method   val   creat   obj   net   eva   text   

原文地址:http://www.cnblogs.com/zxtceq/p/7154378.html

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