Document

.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

posted @ 2017-07-12 11:07  从未被超越  阅读(1158)  评论(0编辑  收藏  举报