【问题标题】:How do I read text directly from a URL using VB.net?如何使用 VB.net 直接从 URL 读取文本?
【发布时间】:2014-10-28 00:38:23
【问题描述】:

我正在制作一个 Windows Phone 8 应用程序。我有目标位置的纬度和经度。我需要获取目标位置的两个字母 ISO 国家代码。

我正在使用以下代码来实现它。

        'Dim address As String = puri
        'Dim client As WebClient = New WebClient()
        'Dim reader As StreamReader = New StreamReader(address)
        'code = reader.ReadToEnd
        Dim inStream As StreamReader
        Dim wr As WebRequest
        Dim webresponse As WebResponse
        wr = WebRequest.Create(puri)
        webresponse = wr.GetResponse()
        inStream = New StreamReader(webresponse.GetResponseStream())
        code = inStream.ReadToEnd()

其中 puri(在注释代码中)是字符串格式的 Web 服务的地址。

尝试注释代码时,我得到的错误是字符串无法转换为 system.uri 格式。 (地址)

在尝试未注释的代码时,我收到一条错误消息,指出 getresponse 不是 system.net.webrequest() 类的成员

我猜随着 .NET 的更新,代码发生了变化,但我找不到任何关于该主题的最新内容。

URI = http://api.geonames.org/countryCode?lat=17.60890&lng=76.98966&username=demo

【问题讨论】:

    标签: vb.net windows-phone-8 webclient downloadstring


    【解决方案1】:

    我认为您应该使用WebClient Class 而不是 WebRequest。它更简单,更快。这是一个简单的例子:

    Dim WebCL As New WebClient
    Dim DownLoadedText As String = String.Empty
    Try
        DownLoadedText = WebCL.DownloadString("Your Url")
        ' Do something 
    Catch ex As Exception
        Throw New Exception("Oops!! ERROR has occured, something is wrong with your address")
    End Try
    

    【讨论】:

    • DownloadString 不是 system.net.webclient 的成员。我在其他地方找到了这段代码。由于此错误而无法正常工作。
    • 其实是....MSDN
    • 它不起作用。我知道文档显示 DownloadString 属于 webclient 类。但智能感知仅显示 WebClient 对象的 DownloadStringAsync。并且仅强制 DownloadString 会产生错误。
    猜你喜欢
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多