1

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.

I just want to send serialized objects to the web service.
Have the web service deserialize the object and make a decision based on it's contents and send back a respond to the application.

I need to start testing the web service and 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 api in vb.net?

शेखर
  • 17,412
  • 13
  • 61
  • 117
  • there are numerous ways to create a web service in .net. Which way are you using? That will help in giving you the best hints on how to consume it. – NotMe Apr 19 '13 at 19:33
  • 1
    How's your vb.net app built? Here's an example on how to consume ASP.NET API via Console app. http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client – Ray Cheng Apr 19 '13 at 19:37
  • That's just it I don't know how. But I'm looking into the link at asp.net –  Apr 19 '13 at 19:55
  • This works. It's what I was looking for almost. I know there isn't a lot of solution written in vb. I can find conversion tools online. Thanks guys. –  Apr 19 '13 at 20:16

2 Answers2

0

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
        reader.Close()
        dataStream.Close()
        response.Close()
    End Sub
End Class
Igor Monteiro
  • 933
  • 1
  • 11
  • 29
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
elarcoiris
  • 1,914
  • 4
  • 28
  • 30