[.net] WebRequest Exception

Started by
6 comments, last by BradSnobar 17 years, 10 months ago
Im trying to get the response of a web request by calling the GetResponse() method after creating (succesfully) the WebRequest. However, one day, all of a sudden, after it had been working earlier, I get a "ReceiveFailure" WebException. Any Ideas?
Advertisement
Could you please post some code so we can take a look?
WebRequest req;
WebResponse res;

req = WebRequest.Create("https://nexus.passport.com/rdr/pprdr.asp");
res = req.GetResponse(); (Throws WebException : ReceiveFailure)
Hey, I'm not sure because your code looks different than I expected, but I do have some web request code that I know works quite well in a testharness that I wrote once.

Let me know if you have questions (I just copied it from inside a method, so I hope that I got all of it in there.)

You'll have to write your own logging routines or replace them with writelines or whatever.

        string link = "http://www.yahoo.com";        DateTime dtnow = DateTime.Now;        //Perform the request for the web page here.        try        {          System.Net.HttpWebRequest myRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(link);  // Initialize the WebRequest.          myRequest.AllowAutoRedirect = true;          myRequest.MaximumAutomaticRedirections = 100;          myRequest.MaximumResponseHeadersLength = 1024;          using (System.Net.HttpWebResponse myResponse = (System.Net.HttpWebResponse)myRequest.GetResponse()) // Return the response.           {            try            {              //Perform validation here.              if (myResponse.StatusCode == System.Net.HttpStatusCode.OK)              {                //Read from the page here.                using (Stream receiveStream = myResponse.GetResponseStream())                {                  try                  {                    //Pipe the receiveStream into streamReader to make this a little easier.                    using (StreamReader sr = new StreamReader(receiveStream, Encoding.UTF8))                    {                      try                      {                        //Read the content off of the page with a stream                        string actualResult = sr.ReadToEnd();                        bool result = false;                        //Validate that some content actually did get returned.                        if (actualResult.Length > 0)                          result = true;                        //Report result                         if (result == true)                          Logger.LogSuccess("Passed :: Link successfully requested :: " + string.Format(Logger.LinkPrintFormat, link));                        else                          Logger.LogError("Failed :: Unable to request link :: " + string.Format(Logger.LinkPrintFormat, link));                        //Report verification results                        if (result == true)                           System.Console.WriteLine("process stuff here");                      }                      finally                      {                        sr.Close();                      }                    }                  }                  finally                  {                    receiveStream.Close();                  }                }              }              else                Logger.LogError("Failed :: " + link);            }            catch (Exception ex)            {              Logger.LogError("Failed :: " + link);              Logger.LogError(ex);            }            finally            {              if (myResponse != null)                myResponse.Close();        // Close the response to free resources.            }          }  //using myResponse        }        catch (System.Net.WebException ex)        {          if (ex.Message == "The remote server returned an error: (400) Bad Request.")          {            Logger.LogError("Failed :: " + link);            Logger.LogError(ex.Message);          }          else          {            Logger.LogError("Failed :: " + link);            Logger.LogError(ex.Message);          }        }        TimeSpan elapsedTime = DateTime.Now.Subtract(dtnow);        if (elapsedTime.Seconds > 10)          Logger.LogInformation("Elapsed Time: " + elapsedTime.Seconds + " seconds");


[Edited by - BradSnobar on June 14, 2006 8:52:42 PM]
Thanks, Ill give it a try :)

One more question though, HttpUtility seems to be a well known class supplied by the System.Web namespace. I cant seem to be able to find it :S
THe HTTPUtility Class should indeed be in the System.Web namespace. There's only one thing I could think of if it is not there:
You need to add a reference to the System.Web Assembly.
Although I guess you've done this already. If it is an ASP.NET project it should have been added at the project creation by Visual Studio.
Nop, it aint there, haha.
Yes, I added the Web namespace, and I do see that objects from the namespace are indeed there. However, the HttpUtility isnt there. Even when I use the object browser to browse for the class I find it, but I cant find it when typing the actual code.

Its extremely weird.
Sometimes intellisense is broken.
That might be what you are encountering?

This topic is closed to new replies.

Advertisement