GetResponse不是UWP中WebRequest的成员

时间:2018-07-10 05:47:01

标签: vb.net uwp

我编写了一个VB程序来从Web访问信息。 它可以工作,但是当尝试在UWP(通用Windows)中做同样的事情时… 我收到以下消息:  “'GetResponse'不是'WebRequest'的成员”

这是我使用的代码,而不是网站信息。在通用Windows平台中如何使用此代码。

Imports System
Imports System.IO
Imports System.Net

Module Code

    Sub SendWebRequest()

        Dim MyWebRequest As WebRequest
        Dim MyWebResponse As WebResponse
        Dim SR As StreamReader
        Dim ReadString As String

        Dim MyCache As New CredentialCache()
        Dim MyCredential As NetworkCredential = MyCache.GetCredential(New Uri("http://xxxxxx.com:xxxx"), "Basic")
        If MyCredential Is Nothing Then
            MyCache.Add(New Uri("http://http://xxxxxx.com:xxxx"), "Basic", New NetworkCredential("UserName", "UsePassWord"))
        End If

        MyWebRequest = WebRequest.Create("http://http://xxxxxx.com:xxxx/xxxx")

        MyWebRequest.Credentials = myCache
        MyWebResponse = MyWebRequest.GetResponse()

        SR = New StreamReader(MyWebResponse.GetResponseStream)
        Do
            ReadString = SR.ReadLine
            If InStr(ReadString, "Listen to the pronunciation of") Then
                'Do Something
                Exit Do
            End If
        Loop Until SR.EndOfStream

        MyWebResponse.Close()
    End Sub
End Module

我通过添加Async和Await进行了更新。 。 。 运行时问题位于MyWebresponse =等待MyWebRequest.GetResponseAsync()

> User-Unandled异常:System.PlatformNotSupportedException:'值'System.Net.CredentialCache'不支持属性'Credentials'。
Async Sub SendWebRequest()
    Dim MyWebRequest As WebRequest
    Dim MyWebResponse As WebResponse
    Dim SR As StreamReader
    Dim ReadString As String
    Dim MyCache As New CredentialCache()
    Dim MyCredential As NetworkCredential = MyCache.GetCredential(New Uri("http://xxxxxx.com:xxxx"), "Basic")
    If MyCredential Is Nothing Then
        MyCache.Add(New Uri("http://http://xxxxxx.com:xxxx"), "Basic", New NetworkCredential("UserName", "UsePassWord"))
    End If
    MyWebRequest = WebRequest.Create("http://http://xxxxxx.org:xxxx/xxxx")
    MyWebRequest.Credentials = myCache
    MyWebResponse = Await MyWebRequest.GetResponseAsync()
    SR = New StreamReader(MyWebResponse.GetResponseStream)
    Do
        ReadString = SR.ReadLine
        If InStr(ReadString, "Listen to the pronunciation of") Then
            'Do Something
            Exit Do
        End If
    Loop Until SR.EndOfStream

    MyWebResponse.Close()
End Sub

如果上面有人看到我在做什么错,我将不胜感激。 同时,我将按照jmcilhinney

的建议尝试HttpClient。

2 个答案:

答案 0 :(得分:0)

理论上,您应该可以更改此设置:

Sub SendWebRequest()

对此

Async Sub SendWebRequest()

这:

MyWebResponse = MyWebRequest.GetResponse()

对此:

MyWebResponse = Await MyWebRequest.GetResponseAsync()

This thread表示可能不起作用。它确实建议使用HttpClient作为替代方案,这意味着代码如下:

Dim client As New HttpClient
Dim response = Await client.GetAsync(New Uri("URL here"))

If response.IsSuccessStatusCode Then
    Dim data = Await response.Content.ReadAsStringAsync()

    For Each line In data.Split({vbLf, vbCrLf}, StringSplitOptions.RemoveEmptyEntries)
        'Use line here.
    Next
End If

请注意,我不确定100%这些'vb'常量是否可以在UWP中使用,但为简单起见,在此使用它们。

答案 1 :(得分:0)

以下内容是我在UWP中所需的。发送Web请求并监视返回。 ...感谢jmcilhinney的帮助

导入System.Net

导入System.Net.Http

导入System.Text

模块代码

Google Chrome

最终模块