【问题标题】:vb.net OpenWeatherMap using HttpWebRequestvb.net OpenWeatherMap 使用 HttpWebRequest
【发布时间】:2014-10-19 09:50:45
【问题描述】:

我无法让 HttpWebRequest 与 OpenWeatherMap 一起正常工作。

当我从浏览器尝试 URL 时,我得到了数据。但是,当我从程序发送它时,我会收到一条带有代码 id 的消息。像这样:

"message":"","cod":"404"

我做错了什么?


VB.NET 代码:

    Private Shared AppID As String = "add_app_id_Here"
Public Shared Function GetWeather(ByVal location As String) As List(Of WeatherDetails)
    Dim url = String.Format _
        ("http://api.openweathermap.org/data/2.5/forecast/daily?q={0}&type=accurate&mode=xml&units=metric&cnt=3&appid={1}",
         location, AppID)
    Try
        Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
        request.AuthenticationLevel = Net.Security.AuthenticationLevel.None
        Dim response As WebResponse = request.GetResponse()
        'The body of the request is sent here
        Dim responseReader As New StreamReader(response.GetResponseStream())
        Dim responseInfo As String = responseReader.ReadToEnd()
        responseReader.Close()
        response.Close()
        If Not (responseInfo.Contains("message") And responseInfo.Contains("cod")) Then
            Dim xEl = XElement.Load(New System.IO.StringReader(responseInfo))
            Return GetWeatherInfo(xEl)
        Else
            Return New List(Of WeatherDetails)
        End If
    Catch ex As Exception
        Return New List(Of WeatherDetails)
    End Try

End Function

【问题讨论】:

    标签: vb.net weather-api openweathermap


    【解决方案1】:

    打断我的脑袋后,我发现他的方法HttpWebRequest 不能容忍特殊字符或不可打印的字符。 因此,我必须在 URL 中硬编码“%27”字符,它解决了问题。

    【讨论】:

      猜你喜欢
      • 2014-02-27
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多