HttpWebRequest and status codes RRS feed

  • Question

  • User1954052445 posted
    I have the following code. try { Uri uri =new Uri(TaskDTO.URL); webReq = (HttpWebRequest)WebRequest.Create(uri); webReq.Timeout=10000; //10 seconds per request webResp = (HttpWebResponse)webReq.GetResponse(); TaskDTO.Status = webResp.StatusCode.ToString(); TaskDTO.Available=true; } catch(Exception HttpEx) { TaskDTO.ErrorMessage =HttpEx.ToString(); //TaskDTO.Status=webResp.StatusCode.ToString(); TaskDTO.Available=false; } If a link is broken I want to store the code returned (404) in the Status property of my taskDTO object, however if the url doesn't exist an exception is thrown on the line: webResp = (HttpWebResponse)webReq.GetResponse(); and the webResp object is null within the catch. If I remove the try/catch the program crashes How do I get the status code??????
    Thursday, September 16, 2004 7:10 AM

All replies