我正在「舊」VB.Net應用程序中工作。我希望打電話給API,檢查我是否得到了404
,解析JSON結果以及您可以使用API調用執行的所有其他常見事情。.NET Framework 4 VB.Net調用API
這樣做的最乾淨的方法是什麼?我以爲我可以使用HttpClient班,但我顯然不能! VS 2017不會讓我選擇將System.Net.Http
作爲導入。
CODE
現在我這樣做,這似乎亂了。
Public Function GetUserInfo(ByVal authTokenBytes As Byte()) As WebPayWS.GetUserInfoResult
Dim token As New token
token.token1 = authTokenBytes
Dim userInformation As GetUserInfoResult = _myService.GetUserInfo(token)
If AccountExists(userInformation.accountno) Then
Dim response As String = GetAccountInfoFromApi(userInformation.accountno)
End If
Return userInformation
End Function
Private Function GetAccountInfoFromApi(accountno As String) As String
Dim accountInformationUrl As String = "URL"
Dim webClient As WebClient = New WebClient()
Return webClient.DownloadString(New Uri(accountInformationUrl))
End Function
我堅持與WebClient
?如果是,我如何使用WebClient檢查404
?
所以確實如此,我不能在.NET Framework v4中使用HttpClient。謝謝@JimmySmith – Ciwan
不,這是不正確的。 – djv
是的,基於問題的第二部分 - 直到4.5之後才能使用HttpClient。但是如果你需要進一步研究,我推薦HttpWebRequest/Response類,https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v=vs.71).aspx –