using System.Net;
using System.Text;
///
///WebClientExt 的摘要说明
///
public class WebClientExt {
public WebClientExt() {
//
//TODO: 在此处添加构造函数逻辑
//
}
///
/// 采用Get方式获取远程页面源代码
///
public static string Get(string uri) {
WebClient wc = new WebClient();
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
wc.Encoding = Encoding.UTF8;
string html = wc.DownloadString(uri);
return html;
}
///
/// 采用Post方式提交数据并获取结果
///
public static string Post(string uri, string data) {
WebClient wc = new WebClient();
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
wc.Encoding = Encoding.UTF8;
wc.Headers.Add("Content-Type:application/x-www-form-urlencoded");
string html = wc.UploadString(uri, data);
return html;
}
///
/// 采用Post方式提交数据并获取结果,第三个参数为认证数据
///
public static string Post(string uri, string data, string auth) {
WebClient wc = new WebClient();
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
wc.Encoding = Encoding.UTF8;
wc.Headers.Add("Content-Type:application/json");
wc.Headers.Add("Authorization", "Basic " + auth);
string html = wc.UploadString(uri, data);
return html;
}
}