WebClient报错——远程主机强迫关闭。。。
使用WebClint经常遇到远程主机强迫关闭现有的链接,一般情况下都是因为WebClient资源不够了,当你再大量的循环的时候,即使使用using也是没用,所以,封装了以下方式,可改善,哈哈
public class xxxHelper { private static WebClient client = new WebClient() { Encoding = System.Text.ASCIIEncoding.UTF8 }; private static void GetWebClient() { client = new WebClient() { Encoding = System.Text.ASCIIEncoding.UTF8 }; } public static books getNovelList(string cono) { books bi = new books(); try { string result = client.DownloadString("http://abc.cn/offer/xxx.php?cono=" + cono); result = result.Replace("<book>", "<ksbook>").Replace("</book>", "</ksbook>"); bi = DeserializeToObject<books>(result); } catch (System.Exception ex) { Tiexue.General.Exception.Error(ex, "WebClient链接异常"); GetWebClient(); string result = client.DownloadString("http://abc.cn/offer/xxx.php?cono=" + cono); result = result.Replace("<book>", "<ksbook>").Replace("</book>", "</ksbook>"); bi = DeserializeToObject<books>(result); } return bi; }
浙公网安备 33010602011771号