您的位置:首页 > 其它

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

2017-07-12 11:07 519 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: