在C#开发中经常需要进行Web远程访问,方法很多,也使用了很久,但一直没有做一个总结。
C#中用来进行远程请求的方法有很多,如WebClient,WebRequest等,也各有特点。今天在这里主要介绍WebRequest。
先从相对入门的不需要证书验证,不需要登录的开始。
//GET类型的请求 public string GetContent(string uri, Encoding coding) { //Get请求中请求参数等直接拼接在url中 WebRequest request = WebRequest.Create(uri); //返回对Internet请求的响应 WebResponse resp = request.GetResponse(); //从网络资源中返回数据流 Stream stream = resp.GetResponseStream(); StreamReader sr = new StreamReader(stream, coding); //将数据流转换文字符串 string result = sr.ReadToEnd(); //关闭流数据 stream.Close(); sr.Close(); return result; }
//POST public string GetContentPost(string uri, string data, Encoding coding) { WebRequest request = WebRequest.Create(uri); request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; //将字符串数据转化为字节串,这也是POST请求与GET请求区别的地方 byte[] buffer = coding.GetBytes(data); //用于将数据写入Internet资源 Stream stream = request.GetRequestStream(); stream.Write(buffer, 0, buffer.Length); request.ContentLength = buffer.Length; WebResponse response = request.GetResponse(); //从网络资源中返回数据流 stream = response.GetResponseStream(); StreamReader sr = new StreamReader(stream, coding); //将数据流转换文字符串 string result = sr.ReadToEnd(); //关闭流数据 stream.Close(); sr.Close(); return result; }
//回调验证证书问题 public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { //直接返回true,接受指定证书进行身份验证 return true; } //Get public string GetContent(string uri, Encoding coding) { //下面一行代码一定卸载请求开始前。 //使用回调的方法进行验证。 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); //Get请求中请求参数等直接拼接在url中 WebRequest request = WebRequest.Create(uri); //返回对Internet请求的响应 WebResponse resp = request.GetResponse(); //从网络资源中返回数据流 Stream stream = resp.GetResponseStream(); StreamReader sr = new StreamReader(stream, coding); //将数据流转换文字符串 string result = sr.ReadToEnd(); //关闭流数据 stream.Close(); sr.Close(); return result; } //POST public string GetContentPost(string uri, string data, Encoding coding) { //下面一行代码一定卸载请求开始前。 //使用回调的方法进行验证。 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); WebRequest request = WebRequest.Create(uri); request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; //将字符串数据转化为字节串,这也是POST请求与GET请求区别的地方 byte[] buffer = coding.GetBytes(data); //用于将数据写入Internet资源 Stream stream = request.GetRequestStream(); stream.Write(buffer, 0, buffer.Length); request.ContentLength = buffer.Length; WebResponse response = request.GetResponse(); //从网络资源中返回数据流 stream = response.GetResponseStream(); StreamReader sr = new StreamReader(stream, coding); //将数据流转换文字符串 string result = sr.ReadToEnd(); //关闭流数据 stream.Close(); sr.Close(); return result; }
//只需在证书验证的回调函数中添加几行代码即可//回调验证证书问题 public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { Console.WriteLine("证书的有效日期:" + certificate.GetEffectiveDateString()); Console.WriteLine("证书的到期日期:" + certificate.GetExpirationDateString()); Console.WriteLine("证书格式名称:" + certificate.GetFormat()); Console.WriteLine("证书办法机构名称:" + certificate.Issuer); Console.WriteLine("密钥算法信息:" + certificate.GetKeyAlgorithm()); Console.WriteLine("证书的公钥:" + certificate.GetPublicKeyString()); Console.WriteLine("证书序列号:" + certificate.GetSerialNumberString()); // 总是接受 return true; }
使用如下GET方式访问百度。即可得到截图效果。
GetContent("https://www.baidu.com/", Encoding.UTF8);
Console.WriteLine(str);
来源:https://www.cnblogs.com/DSC1991/p/12552012.html