dcsimg
Results 1 to 2 of 2

Thread: Api Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Location
    Ohio
    Posts
    172

    Api Question

    Hey everyone I am wondering how html api works with vb? For example on a gaming website I seen they had an api and it says this "You can access the API via. HTTP POST over following URL:" With the post method would that be a webrequest in vb?

    I am wanting to learn how to use api from some websites so if someone could just give me an explanation to this that would be awesome No need to provide me any code just a simple explanation would do.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Api Question

    You can create GET and POST web requests using System.Net.WebRequest class. The procedure is as such:
    1. You create a webrequest using
    Code:
    Dim wrq As System.Net.WebRequest = System.Net.HttpWebRequest.Create("your url here")
    2. You specify the method of the request (GET or POST)
    Code:
    wrq.Method = WebRequestMethods.Http.Post
    You don't have to specify the method if you're using GET since it's the default value and you completely skip the steps 3 and 4.

    3. Since it's a POST request you should obtain the request stream using
    Code:
    Dim rqstr As IO.Stream = wrq.GetRequestStream()
    4. You write your POST data (setting=value) into the request stream
    Code:
    Using sw As New IO.StreamWriter(rqstr)
        sw.WriteLine("setting=value") ' etc (you should refer to the API docs)
    End Using
    5. Once you've done you can obtain the server response
    Code:
    Dim rsp As System.Net.WebResponse
    Try
        rsp = wrq.GetResponse()
    Catch ex As Exception
        ' Handle errors here
    End Try
    This is actually the moment the outbound connection is established. Always enclose the call to the GetResponse() method into the Try...Catch block to catch possible exeption
    6. Your response object (System.Net.WebResponse) will have the GetResponseStream() method whilch will allow you to get a stream with the data that had been returned from the server.
    Code:
    Dim htmlsource As String = ""
    Using sr As New IO.StreamReader(rsp.GetResponseStream())
         htmlsource = sr.ReadToEnd()
    End Using
    7. Finally, you close the response
    Code:
    rsp.Close()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width