1

就像我最初的问题一样。
我想知道在vb.net 中有一个用于使用 asp.net web api 的内容
我是初学者 asp.net 程序员。
我正在观看有关如何制作 web api 的视频。
但似乎找不到任何关于在 vb.net 应用程序中使用 Web 服务的迹象。

我只想发送serialized objectsweb service.
拥有web service deserialize对象并根据其内容做出决定并将响应发送回应用程序。

我需要开始测试,web service并且我想让我的应用程序与api.
这又回到了这个问题。如何从web apiin发送和接收数据vb.net

4

2 回答 2

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
于 2016-01-27T14:24:46.227 回答
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
于 2020-03-09T02:39:27.063 回答