跳转到帖子
登录关注  
墨香年少

c#获取html

已推荐帖子

private string getHTML(string url)
        {
            if (url.Substring(0, 4) != "http")
            {
                return "error";
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode.ToString() != "OK")
                {
                    return "error";
                }

                if (response.StatusCode == HttpStatusCode.NotFound)
                    return "error";
                if (response.StatusCode == HttpStatusCode.InternalServerError)
                    return "error";
                if (response.StatusCode == HttpStatusCode.BadGateway)
                    return "error";
                Stream ResponseStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(ResponseStream, Encoding.UTF8);
                string html = sr.ReadToEnd();
                sr.Close();
                ResponseStream.Close();
                return html;
            }
            catch (Exception e)
            {
                return "error";
            }
            return "error";
        }

 


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

创建帐户或登录来提出意见

你需要成为会员才能提出意见

创建帐户

注册成为会员。只要几个简单步骤!

注册帐户

登录

已有帐户? 请登录。

现在登录
登录关注  

×
×
  • 创建新的...

重要信息

注册必须使用2-8个中文汉字作为账号