我想我在这里犯了一个可怕的错误...
Dim s As String = GetResponse("", New KeyValuePair(Of String, String))
Public Async Function GetResponse(ByVal url As String, ByVal params As KeyValuePair(Of String, String)) As System.Threading.Tasks.Task(Of String)
Dim values = New List(Of KeyValuePair(Of String, String))
Dim httpClient = New HttpClient(New HttpClientHandler())
Dim response As HttpResponseMessage = Await httpClient.PostAsync(url, New FormUrlEncodedContent(values))
response.EnsureSuccessStatusCode()
Return Await response.Content.ReadAsStringAsync()
End Function
有什么想法吗?
最佳答案
您的函数返回的不是字符串。它返回一个Task(Of String),就像在错误消息中以及在函数声明中所看到的那样。您会发现,它没有按照您的想法工作。
解决方案应该是:尝试调用GetResponse("", New KeyValuePair(Of String, String)).Result
,它将完成您想要的操作。结果是类似于Task的type参数的类型。
关于vb.net - 类型 'System.Threading.Tasks.Task(Of String)'的值不能转换为 'String',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23117055/