WebClient client = new WebClient(); try { string res = ""; System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; if (IsHead) { client.Headers.Add("X-Requested-With", "XMLHttpRequest"); } Stream stream = client.OpenRead(url); StreamReader sr = new StreamReader(stream, Encoding.UTF8); res = sr.ReadToEnd(); sr.Close(); client.Dispose(); return res; } catch (Exception ex) { Log.Error(string.Format("请求url:{0},错误信息:{1}", url, ex.ToString())); client.Dispose(); return ""; }
在使用这段代码进行做爬虫请求数据的时候,报异常输流收到意外的 EOF 或 0 个字节 的异常。最后的解决方法参考了以下链接。
https://www.jianshu.com/p/5d2867435a6e