有没有人知道如何在VB.Net for Windows Phone 8中执行异步发布请求?
我尝试了很多,但没有任何效果......这个http://msdn.microsoft.com/de-de/library/system.net.httpwebrequest.begingetrequeststream.aspx也不起作用。
非常感谢。
答案 0 :(得分:2)
我不得不为我自己解决这个问题。让我看看我能做些什么来帮助你。
发布网络请求实际上比链接显示的要简单。这就是我的工作。
首先,我创建一个MultipartFormDataContent:
Dim form as New MultipartFormDataContent()
接下来,我添加我想要发送的每个字符串:
form.Add(New StringContent("String to sent"), "name of the string you are sending")
接下来,创建一个HttpClient:
Dim httpClient as HttpClient = new HttpClient()
接下来,我们将创建一个HttpResponseMessage并将您的信息发布到您选择的网址:
Dim response as HttpResponseMessage = Await httpClient.PostAsync("www.yoururl.com/wherever", form)
然后,我通常需要将响应作为字符串,所以我读取了对字符串的响应:
Dim responseString as String = Await response.Content.ReadAsStringAsync()
这将为您提供您想要的响应,如果这是您想要的。
以下是我使用的方法示例:
Public Async Function GetItems() As Task
Dim getUrl As String = "https://myapiurl.com/v3/get"
Dim responseText As String = String.Empty
Dim detailType As String = "complete"
Try
Dim httpClient As HttpClient = New HttpClient()
Dim form As New MultipartFormDataContent()
form.Add(New StringContent(roamingSettings.Values("ConsumerKey").ToString()), "consumer_key")
form.Add(New StringContent(roamingSettings.Values("access_token").ToString()), "access_token")
form.Add(New StringContent(detailType.ToString()), "detailType")
Dim response As HttpResponseMessage = Await httpClient.PostAsync(getUrl, form)
responseText = Await response.Content.ReadAsStringAsync()
Catch ex As Exception
End Try
End Function
如果您没有使用Http客户端库,则需要像下面这样安装它们: 使用HttpClient需要做的是在Visual Studio中导航,转到Tools-> Library Package Manager->管理此解决方案的Nuget包。在那里,搜索HttpClient的在线部分,并确保在结果上方的列表框中选择“包含预发布”。 (默认设置为“仅稳定”) 然后安装ID为Microsoft.Net.Http
的包然后,您需要在正在使用它的文档的开头添加Import语句。
如果您正在寻找,请告诉我。
谢谢, SonofNun