JavaEar 专注于收集分享传播有价值的技术资料

1个回答

    最佳答案
  1. You cant get it from the webclient however on your WebException you can access the Response Object cast that into a HttpWebResponse object and you will be able to access the entire response object.

    Please see the WebException class definition for more information.

    Below is an example from MSDN (is not the best way to handle the exception but it should give you some idea)

    try {
       // Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name.
         HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("invalid site");
    
        // Get the associated response for the above request.
         HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
        myHttpWebResponse.Close();
    }
    catch(WebException e) {
        Console.WriteLine("This program is expected to throw WebException on successful run."+
                            "\n\nException Message :" + e.Message);
        if(e.Status == WebExceptionStatus.ProtocolError) {
            Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
            Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
        }
    }
    catch(Exception e) {
        Console.WriteLine(e.Message);
    }