.Net 请求Web接口Post和Get方法
#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