Hi
I think you may take a look at the code below for your reference.
I can not test based on all the possibility, but commonly if the
request.GetResponse(); throw exception, the response will be null;
static void Main(string[] args)
{
// Create a request for the URL.
WebRequest request = WebRequest.Create(
"http://localhost");
// If required by the server, set the credentials.
// request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = null;
try
{
// Get the response.
response = request.GetResponse();
// Display the status.
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
if (response != null)
{
Console.WriteLine(((HttpWebResponse)response).Stat usDescription);
// Get the stream containing content returned by the
server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams and the response.
reader.Close();
response.Close();
}
}
Also from VB.NET 2005, we have a continue keyword, which can be used to go
the next iteration of the loop without go through the all the following
code.
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.