首页 新闻 搜索 专区 学院

.net mvc远程抓去其它网站的html

0
悬赏园豆:30 [已解决问题] 解决于 2014-06-08 14:29

.net mvc怎么样才能抓取其它网站的html片段呢?jquery ajax的load,ajax请求受同源策略限制,无法获取远程的html,还有其它方法可以实现吗?

远程抓取内容的网站没法做任何修改,相当于远程抓取html代码片段。

智伟的主页 智伟 | 菜鸟二级 | 园豆:266
提问于:2014-05-22 23:08
< >
分享
最佳答案
0
HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            try
            {
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = contentType;
                httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
                httpWebRequest.Referer = url;
                httpWebRequest.Accept = accept;
                httpWebRequest.UserAgent = userAgent;
                httpWebRequest.Method = "GET";
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, encoding);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                currentTry--;
                httpWebRequest.Abort();
                httpWebResponse.Close();
                return html;
            }
            catch (Exception e)
            {
                if (currentTry <= maxTry) GetHtml(url, cookieContainer);
                currentTry--;
                if (httpWebRequest != null) httpWebRequest.Abort();
                if (httpWebResponse != null) httpWebResponse.Close();
                return string.Empty;
            }
收获园豆:15
贺臣 | 初学一级 |园豆:107 | 2014-05-23 17:51
其他回答(1)
0

使用服务端来获取;服务端.net封装的有HttpWebRequest类,可实现该功能

收获园豆:15
Devin Mao | 园豆:596 (小虾三级) | 2014-05-23 09:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册