Sign in to follow this  

[.net] WebRequest Exception

This topic is 4196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
WebRequest req;
WebResponse res;

req = WebRequest.Create("https://nexus.passport.com/rdr/pprdr.asp");
res = req.GetResponse(); (Throws WebException : ReceiveFailure)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this