Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to implement status code response from the web request and I am currently experiencing the following error on the line of code below:

error --> Cannot implicitly convert type 'System.Net.HttpStatusCode' to 'int'. An explicit conversion exists (are you missing a cast?)
statusCode = ((HttpWebResponse)e.Response).StatusCode;

Entire code:
C#
public static string WRequest()
        {
           string URL = "########";
           string method = "POST";
           string postData = newTemplates.createXMLIssue();

            string responseData = "";
            int statusCode;

            try
            {
                HttpWebRequest hwrequest = (HttpWebRequest)WebRequest.Create(URL);
                hwrequest.Timeout = 60000;
                hwrequest.Method = method;
                hwrequest.KeepAlive = false;

                if (hwrequest.Method == "POST")
                {
					//additonal code
                }

                // Attempt to receive the WebResponse to the WebRequest.
                using (HttpWebResponse hwresponse = (HttpWebResponse)hwrequest.GetResponse())
                {
                    statusCode = (int)hwresponse.StatusCode;
                    if (hwresponse != null)
                    { // If we have valid WebResponse then read it.
                        using (StreamReader reader = new StreamReader(hwresponse.GetResponseStream()))
                        {
                           // XPathDocument doc = new XPathDocument(reader);
                            string responseFromServer = reader.ReadToEnd();
                           // XPathNavigator xml = doc.CreateNavigator();
                            //responseData = xml.ToString().Trim();
                            responseData = responseFromServer;
                            reader.Close();
                        }
                    }
                    hwresponse.Close();
                }
            }
            catch (WebException e)
            {
                string message = ((HttpWebResponse)e.Response).StatusDescription;
                statusCode = ((HttpWebResponse)e.Response).StatusCode;
                responseData = "An error occurred: " + message + statusCode;
            }
            return responseData;
        }
    }


Any hints, to where I may be going wrong, would be very much appreciated. Many thanks.
Posted
Updated 6-Apr-16 18:38pm
Comments
[no name] 15-Sep-14 7:26am    
Why can you cast it in one place, statusCode = (int)hwresponse.StatusCode;, but not another?
StatusCode is an Enum

The error message tells you the problem. You are trying to convert the value to an int.

StatusCode is of type HttpStatusCode(an Enum)[^] which won't convert unless you explicitly cast it.

You could do:

C#
statusCode = (int)((HttpWebResponse)e.Response).StatusCode;

which is exactly what you are doing here:
Quote:
C#
statusCode = (int)hwresponse.StatusCode;
 
Share this answer
 
what is the value of e in the following statement..? where it is declared..?

C#
statusCode = (int)((HttpWebResponse)e.Response).StatusCode;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900