就像我原来的问题所说的那样
我想知道有一个 a在vb.net中使用asp.net web api 。
我是一名初学的asp.net程序员。
我正在观看有关如何制作网络API的视频。
但似乎无法找到在vb.net应用程序中使用Web服务的任何迹象。
我只想将serialized objects
发送到web service
。
拥有web service deserialize
对象并根据其内容做出决定并发回对应用程序的响应。
我需要开始测试web service
,我希望让我的应用程序与api
一起使用。
哪个循环回到问题。如何从web api
中的vb.net
发送和接收数据?
答案 0 :(得分:0)
帖子例子:
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
reader.Close()
dataStream.Close()
response.Close()
End Sub
End Class
答案 1 :(得分:0)
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