Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to read a specific url and read the contents of that page, but it redirects to other url which causes issue. How can i stop or read from the url i want.
I have used the follwing method to retrive specific tag which failed to do so.
C#
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("http://www.xxxxxxxx.aspx");
                HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
                if (response1.StatusCode == HttpStatusCode.OK)
                {
                    Stream receiveStream = response1.GetResponseStream();
                    StreamReader readStream = null;
                    if (response1.CharacterSet == null)
                    {
                        readStream = new StreamReader(receiveStream);
                    }
                    else
                    {
                        readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response1.CharacterSet));
                    }
                    string data = readStream.ReadToEnd();
                    response1.Close();
                    readStream.Close();
                }
Posted
Updated 17-Mar-15 4:01am
v2
Comments
ZurdoDev 17-Mar-15 10:07am    
Why is it redirecting? It's likely an authentication issue.

1 solution

Set the AllowAutoRedirect property[^] to false:
C#
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("http://www.xxxxxxxx.aspx");
request1.AllowAutoRedirect = false;
...
 
Share this answer
 
Comments
Tapan Kumar Panda 18-Mar-15 3:47am    
I have already tried but not able get appropriate result.
request1.Method = "GET";
request1.AllowAutoRedirect = false;

But done with a php solution. May be they are using session or cookies to store values?!

I am trying this from http://secure.newegg.com/
After add to cart , when i check the view cart, I need the "IN STOCK" value.
Richard Deeming 18-Mar-15 8:47am    
If it's a shopping cart, then the problem is unlikely to be a redirection. It's more likely that they're using cookies to identify the basket; when you request the page without the cookies, you'll get a new empty basket.

Use Fiddler[^] to see what the request and response look like from your code, and from a web browser.

Assuming the problem is due to the cookies, you'll need to set a CookieContainer on your HttpWebRequest. Make sure you use the same container instance for both the request to add an item to the basket, and the request to read the contents of the basket.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900