如何解决System.Net.HttpStatusCode错误? [英] How to solve System.Net.HttpStatusCode error ?

查看:116
本文介绍了如何解决System.Net.HttpStatusCode错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网络请求实施状态代码响应,我目前在以下代码行中遇到以下错误:



错误 - > ; 无法将类型'System.Net.HttpStatusCode'隐式转换为'int'。存在显式转换(您是否错过了演员?)

 statusCode =((HttpWebResponse)e.Response).StatusCode; 



整个代码:

  public  < span class =code-keyword> 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
}

// 尝试接收WebRequest的WebResponse。
使用(HttpWebResponse hwresponse =(HttpWebResponse)hwrequest.GetResponse())
{
statusCode =( INT )hwresponse.StatusCode;
if (hwresponse!= null
{ // 如果我们有有效的WebResponse,那么请阅读它。
使用(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 = 发生错误: + message + statusCode;
}
return responseData;
}
}





任何提示,我可能出错的地方,都会非常感激。非常感谢。

解决方案

错误消息告诉您问题。您正在尝试将值转换为int。



StatusCode的类型为 HttpStatusCode(一个枚举) [ ^ ]除非您明确投射,否则不会转换。



你可以这样做:



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



这正是你在这里做的:

< blockquote class =quote>

Quote:

 statusCode =( int )hwresponse。 StatusCode; 


以下语句中e的值是多少?它被声明的地方..?



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


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:

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.

解决方案

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:

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


which is exactly what you are doing here:

Quote:

statusCode = (int)hwresponse.StatusCode;


what is the value of e in the following statement..? where it is declared..?

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


这篇关于如何解决System.Net.HttpStatusCode错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆