6

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);
   }
2
  • you want to send webrequest from that url right ?? Commented Jul 20, 2012 at 6:31
  • AFAIK you have to call wrGETURL.GetResponse() before using anything within the response Commented Jul 20, 2012 at 6:40

3 Answers 3

11

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes it is working now but if i need to get response for subway.com/storelocator/default.aspx?zip=04416&country=USA this url it not gives all store what i do to get all store from subway if we specify miles distance
@Janhavi Your question was I have to read response from .. and you got it. You should know why your url doesn't give the data you expect.
3
Stream objStream;
StreamReader objSR;
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

string str = "http://domaninname.com/YourPage.aspx?name=" + "abc";
HttpWebRequest wrquest = (HttpWebRequest)WebRequest.Create(str);
HttpWebResponse getresponse = null;
getresponse = (HttpWebResponse)wrquest.GetResponse();

objStream = getresponse.GetResponseStream();
objSR = new StreamReader(objStream, encode, true);
string strResponse = objSR.ReadToEnd();
Response.Write(strResponse);

Comments

0

In just one line, with easier vb.net (that gets same performance as c#)

Return New System.IO.StreamReader(System.Net.HttpWebRequest.Create("http://urltogetthecontents.com").GetResponse().GetResponseStream()).ReadToEnd()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.