简体   繁体   中英

Unexpected error when calling Weather Web Service from a Web Form

I am attempting to call the weather web service from a web form (ASPX Page). The expected result be an XML response with the weather forecast. However, when I run the web form in the browser I get an error "Server error in '/' Application.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Home/WeatherServiceForm

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4075.0 "

I believe there is no error in my codes and can only think of two possible reasons for the error. First being the incorrect Framework, Second the version of the url path.

I have attempted changing the framework I am using. From "ASP.NET Core Web Application -> ASP.NET Web Application(.NET Framework) I am using Visual Studios 2019 and it automatically generated MVC code type.

Secondly url.Path = "premium/v2/weather.ashx"; The url.Path should either be v1 or v2 I am uncertain.

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;
        }
    }
}

Have you tried requesting the filename verbatim, crucially including the file extension, for example: /WeatherServiceForm.aspx

Requested URL: /Home/WeatherServiceForm appears to be a MVC style route, but if you are using.aspx then I would expect it to use the filename.

"ASP.NET Core Web Application -> ASP.NET Web Application(.NET Framework) I am using Visual Studios 2019 and it automatically generated MVC code type."

THe above template is correct.

404 means nor resource at the server side.

right click the web form in the project, and choose "view in browser" to run the specific page(web form in this case).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM