如何在 vb.net 应用程序中使用 asp.net web api
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16112200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to consume an asp.net web api in vb.net application
提问by DylanTheSoldier
Just like my original question states.
I was wondering in there is a was in vb.net to consume an asp.net web api.
I'm a beginner asp.net programmer.
I'm watching videos on how to make the web api.
But can't seem to find any indication on using the web service in a vb.net application.
就像我原来的问题所说的那样。
我想知道在vb.net中有一个is来使用 asp.net web api。
我是一个初学者 asp.net 程序员。
我正在观看有关如何制作 web api 的视频。
但似乎无法找到有关在 vb.net 应用程序中使用 Web 服务的任何指示。
I just want to send serialized objectsto the web service.
Have the web service deserializethe object and make a decision based on it's contents and send back a respond to the application.
我只想发送serialized objects到web service.
拥有web service deserialize对象并根据其内容做出决定并向应用程序发回响应。
I need to start testing the web serviceand I want to have my application to work with the api.
Which loops back to the question. How do I send and receive data from the web apiin vb.net?
我需要开始测试web service并且我想让我的应用程序与api.
这又回到了这个问题。如何从web apiin发送和接收数据vb.net?
回答by Igor Monteiro
A Post exemple:
一个帖子示例:
Public Class Form1
Private Sub submit_Click(sender As System.Object, e As System.EventArgs) Handles submit.Click
Dim user As String
Dim pass As String
user = uname.Text
pass = passwd.Text
Dim request As WebRequest = WebRequest.Create("http://domain.com/test.php")
request.Method = "POST"
Dim postData As String
postData = "username=" & user & "&password=" & pass
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream()
do what you want with the stream
do what you want with the stream
reader.Close()
dataStream.Close()
response.Close()
End Sub
End Class
回答by user3652935
Private Function PostApiCall(txtV As String) As String
Try
Dim endPoint As String = "https://gorest.co.in/public-api/users"
'Contruct Json Request
Dim dictData As New Dictionary(Of String, Object)
dictData.Add("FetchStart", txtV)
dictData.Add("FetchSize", "uday")
dictData.Add("CustomerName", "gundeti")
dictData.Add("gender", "male")
'Params
Dim reqString() As Byte
Dim resByte As Byte()
Dim responseFromApi As String
Dim client As WebClient = New WebClient()
client.Headers("Content-type") = "application/json"
'client.Headers("Authorization") = "Basic " & Convert.ToBase64String(Encoding.[Default].GetBytes("username:password"))
client.Headers("Authorization") = "Bearer elHd6Cv3Ae2P70mPvfPx9gNnjfbHU-kd9FID"
client.Encoding = Encoding.UTF8
Dim jsonReq = JsonConvert.SerializeObject(dictData, Formatting.Indented)
reqString = Encoding.Default.GetBytes(jsonReq)
resByte = client.UploadData(endPoint, "post", reqString)
responseFromApi = Encoding.Default.GetString(resByte)
Return responseFromApi
Catch ex As Exception
Throw (ex)
End Try
End Function

