打开网页出现乱字符是怎么回事情?
- 提问者网友:藍了天白赴美
- 2021-01-02 11:58
- 五星知识达人网友:撞了怀
- 2021-01-06 20:04
- 1楼网友:青灯有味
- 2021-01-06 20:38
方法一:webrequest
private string getstringbyurl(string strurl) { webrequest wrt = webrequest.create(strurl); webresponse wrse = wrt.getresponse(); stream strm = wrse.getresponsestream(); streamreader sr = new streamreader(strm, encoding.getencoding("gb2312")); string strallstrm = sr.readtoend(); return strallstrm; }
方法二:httpwebrequest
public static string getpage(string url, encoding encoding) { httpwebrequest request = null; httpwebresponse response = null; streamreader reader = null; try { request = (httpwebrequest)webrequest.create(url); request.useragent = "www.svnhost.cn"; request.timeout = 20000; request.allowautoredirect = false; response = (httpwebresponse)request.getresponse(); if (response.statuscode == httpstatuscode.ok && response.contentlength < 1024 * 1024) { reader = new streamreader(response.getresponsestream(), encoding); string html = reader.readtoend(); return html; } } catch { } finally { if (response != null) { response.close(); response = null; } if (reader != null) reader.close(); if (request != null) request = null; } return string.empty; }