C# 从Web窗体调用Weather Web服务时出现意外错误

C# 从Web窗体调用Weather Web服务时出现意外错误,c#,asp.net,C#,Asp.net,我正在尝试从web表单(ASPX页面)调用天气web服务。预期结果是天气预报的XML响应。但是,当我在浏览器中运行web表单时,我在“/”应用程序中得到一个错误“服务器错误” 描述:HTTP 404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下URL并确保其拼写正确 请求的URL:/Home/WeatherServiceForm 版本信息:Microsoft.NET Framework版本:4.0.30319;ASP.NET版本:4.8.4075.0“ 我相

我正在尝试从web表单(ASPX页面)调用天气web服务。预期结果是天气预报的XML响应。但是,当我在浏览器中运行web表单时,我在“/”应用程序中得到一个错误“服务器错误”

描述:HTTP 404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下URL并确保其拼写正确

请求的URL:/Home/WeatherServiceForm

版本信息:Microsoft.NET Framework版本:4.0.30319;ASP.NET版本:4.8.4075.0“

我相信我的代码中没有错误,我只能想到两个可能的错误原因。首先是不正确的框架,其次是url路径的版本

我已尝试更改我正在使用的框架。在“ASP.NET核心Web应用程序->ASP.NET Web应用程序(.NET Framework)”中,我使用的是Visual Studio 2019,它会自动生成MVC代码类型

其次 url.Path=“premium/v2/weather.ashx”; 路径应该是v1或v2,我不确定

protected void Page_Load(object sender, EventArgs e)
    {
        XmlDocument wsResponseXmlDoc = new XmlDocument();

        //http://api.worldweatheronline.com/premium/v1/weather.ashx?key=****&q=London&format=xml&num_of_days=5
        //id=jipx(spacetime0)
        UriBuilder url = new UriBuilder();
        url.Scheme = "http";// Same as "http://"

        url.Host = "api.worldweatheronline.com";
        url.Path = "premium/v2/weather.ashx";// change to v2
        url.Query = "q=china&format=xml&num_of_days=5&key=******";

        //Make a HTTP request to the global weather web service
        wsResponseXmlDoc = MakeRequest(url.ToString());
        if (wsResponseXmlDoc != null)
        {
            //display the XML response for user
            String xmlString = wsResponseXmlDoc.InnerXml;
            Response.ContentType = "text/xml";
            Response.Write(xmlString);

            // Save the document to a file and auto-indent the output.
            XmlTextWriter writer = new XmlTextWriter(Server.MapPath("xmlweather.xml"), null);
            writer.Formatting = Formatting.Indented;
            wsResponseXmlDoc.Save(writer);

            //You're never closing the writer, so I would expect it to keep the file open. That will stop future attempts to open the file

            writer.Close();
        }
        else
        {
            Response.ContentType = "text/html";
            Response.Write("<h2> error  accessing web service </h2>");
        }
    }

    public static XmlDocument MakeRequest(string requestUrl)
    {
        try
        {
            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
            //Set time out to 15 seconds
            request.Timeout = 15 * 1000;
            request.KeepAlive = false;
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(response.GetResponseStream());
            return (xmlDoc);

        }
        catch (Exception ex)
        {
            return null;
        }
    }
}
受保护的无效页面加载(对象发送方,事件参数e)
{
XmlDocument wsResponseXmlDoc=新的XmlDocument();
//http://api.worldweatheronline.com/premium/v1/weather.ashx?key=****&q=London&format=xml&num_of_days=5
//id=jipx(spacetime0)
UriBuilder url=新的UriBuilder();
url.Scheme=“http”;//与“http://”相同
url.Host=“api.worldweatheronline.com”;
url.Path=“premium/v2/weather.ashx”//更改为v2
url.Query=“q=china&format=xml&num\u of_days=5&key=*****”;
//向全球天气web服务发出HTTP请求
wsResponseXmlDoc=MakeRequest(url.ToString());
如果(wsResponseXmlDoc!=null)
{
//为用户显示XML响应
字符串xmlString=wsResponseXmlDoc.InnerXml;
Response.ContentType=“text/xml”;
Write(xmlString);
//将文档保存到文件并自动缩进输出。
XmlTextWriter=newXMLTextWriter(Server.MapPath(“xmlweather.xml”),null;
writer.Formatting=格式化.缩进;
wsResponseXmlDoc.Save(编写器);
//您从未关闭写入程序,因此我希望它保持文件打开。这将阻止以后尝试打开文件
writer.Close();
}
其他的
{
Response.ContentType=“text/html”;
Write(“访问web服务时出错”);
}
}
公共静态XmlDocument MakeRequest(字符串请求URL)
{
尝试
{
HttpWebRequest-request=WebRequest.Create(requestUrl)作为HttpWebRequest;
//将时间设置为15秒
请求超时=15*1000;
request.KeepAlive=false;
HttpWebResponse=request.GetResponse()作为HttpWebResponse;
XmlDocument xmlDoc=新的XmlDocument();
Load(response.GetResponseStream());
退货(xmlDoc);
}
捕获(例外情况除外)
{
返回null;
}
}
}

您是否尝试过逐字请求文件名,包括文件扩展名,例如:/WeatherServiceForm.aspx

请求的URL:/Home/WeatherServiceForm似乎是MVC样式的路由,但如果您使用的是.aspx,则我希望它使用文件名。

“ASP.NET核心Web应用程序->ASP.NET Web应用程序(.NET Framework)我使用的是Visual Studio 2019,它会自动生成MVC代码类型。”

以上模板是正确的

404表示服务器端的nor资源

在项目中的web表单上单击鼠标右键,然后选择“在浏览器中查看”以运行特定页面(本例中为web表单)