本文介绍了使用HTTP Web请求的响应读取URL从的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要读从。我已经用下面的代码,但并没有得到所有的响应。而不是它给提前错误页面谢谢你的回应。

 保护无效的Page_Load(对象发件人,EventArgs五)
{

串SURL =htt​​p://www.subway.com/storelocator/default.aspx?zip=04416&country=USA;
XmlDocument的rssDoc =新的XmlDocument();
XmlTextReader的rssReader = XmlTextReader的新(sUrl.ToString());

的WebRequest wrGETURL;
wrGETURL = WebRequest.Create(SURL);

流objStream;
objStream = wrGETURL.GetResponse()GetResponseStream();
StreamReader的objReader =新的StreamReader(objStream,Encoding.UTF8);
WebResponse的WR = wrGETURL.GetResponse();
流receiveStream = wr.GetResponseStream();
StreamReader的读者=新的StreamReader(receiveStream,Encoding.UTF8);
字符串内容= reader.ReadToEnd();
的Response.Write(内容);
}


解决方案

我不知道是什么你想与的XmlTextReader 做到,因为返回的内容是 HTML 不是 XML ,但是设置用户代理是足以让页

  HttpWebRequest的REQ =(HttpWebRequest的)HttpWebRequest.Create(HTTP: //www.subway.com/storelocator/default.aspx?zip=04416&country=USA); 
req.UserAgent =Mozilla的/ 4.0(兼容; MSIE 8.0; Windows NT的6.1; WOW64;三叉戟/ 4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729 ; .NET4.0C; .NET4.0E);使用
(VAR RESP = req.GetResponse())
{
变种HTML =新的StreamReader(resp.GetResponseStream())为ReadToEnd()。
}


I have to read response from http://www.subway.com/storelocator/default.aspx?zip=04416&country=USA .I have used following code but does not get all the response. instead of it gives response of error page Thanks in advance.

protected void Page_Load(object sender, EventArgs e)
{

    string sUrl = "http://www.subway.com/storelocator/default.aspx?zip=04416&country=USA";
    XmlDocument rssDoc = new XmlDocument();
    XmlTextReader rssReader = new XmlTextReader(sUrl.ToString());

    WebRequest wrGETURL;
    wrGETURL = WebRequest.Create(sUrl);

    Stream objStream;
    objStream = wrGETURL.GetResponse().GetResponseStream();
    StreamReader objReader = new StreamReader(objStream, Encoding.UTF8);
    WebResponse wr = wrGETURL.GetResponse();
    Stream receiveStream = wr.GetResponseStream();
    StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
    string content = reader.ReadToEnd();
    Response.Write(content);
   }
解决方案

I don't know what you want to do with XmlTextReader since returned content is html not xml, however setting UserAgent is enough to get the page.

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.subway.com/storelocator/default.aspx?zip=04416&country=USA");
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
using (var resp = req.GetResponse())
{
    var html = new StreamReader(resp.GetResponseStream()).ReadToEnd();
}

这篇关于使用HTTP Web请求的响应读取URL从的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:35