1个回答
展开全部
我不知道你的代码是什么样的出了异常,给你一段代码供参考: string sRet = ""; try { //目标网址事先存在字符串 sPage 中 HttpWebRequest Myrq = (HttpWebRequest)HttpWebRequest.Create(sPage ); Myrq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; Myrq.Accept = "*/*"; Myrq.Method = "GET"; Myrq.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-cn"); Myrq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); Myrq.KeepAlive = true; //Myrq.Referer = xmlurlrefer;//有些网址要求指定referer //有些网页需要cookie的 //Myrq.Headers.Add(HttpRequestHeader.Cookie, sCookie); HttpWebResponse myrp = (HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; Encoding ec; if (!string.IsNullOrEmpty(myrp.CharacterSet)) { ec = Encoding.GetEncoding(myrp.CharacterSet);//如果响应的网页头有指定编码就用指定编码,否则采用默认的GB2312编码 } else { ec = Encoding.GetEncoding("GB2312"); } Stream st = myrp.GetResponseStream(); using (StreamReader sr = new StreamReader(st,ec )) { sRet = sr.ReadToEnd(); } st.Close(); //Console.WriteLine(sRet); return sRet; } catch (WebException ex) { Console.WriteLine(ex.ToString()); return sRet; }
求采纳
求采纳
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询