110,534
社区成员
发帖
与我相关
我的任务
分享
public static void GetPage(String url)
{
try
{
Uri ourUri = new Uri(url);
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(ourUri);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Console.WriteLine("\nThe server did not issue any challenge. Please try again with a protected resource URL.");
// Releases the resources of the response.
myHttpWebResponse.Close();
}
catch(WebException e)
{
HttpWebResponse response = (HttpWebResponse)e.Response;
if (response != null)
{
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
string challenge = null;
challenge= response.GetResponseHeader("WWW-Authenticate");
if (challenge != null)
Console.WriteLine("\nThe following challenge was raised by the server:{0}",challenge);
}
else
Console.WriteLine("\nThe following WebException was raised : {0}",e.Message);
}
else
Console.WriteLine("\nResponse Received from server was null");
}
catch(Exception e)
{
Console.WriteLine("\nThe following Exception was raised : {0}",e.Message);
}
}
}