Results 1 to 7 of 7

Thread: [RESOLVED] Calling REST API Help

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    India
    Posts
    526

    Resolved [RESOLVED] Calling REST API Help

    Hello Everyone,

    I use .NET Framework 4.0 for coding (VS 2010).


    Code:
      Try
                Dim myReq As HttpWebRequest
                Dim myResp As HttpWebResponse
                Dim myReader As StreamReader
                Dim MMAPIURL As String = "https://restapi.mm.com/signal", Token as string = "123456678", CT as string ="BUY EXIT"
    
                myReq = HttpWebRequest.Create(MMAPIURL)
                myReq.Method = "POST"
                myReq.ContentType = "application/json"
                myReq.Accept = "application/json"
                Dim CT As String = Call_Type
                Dim myData As String = "{""token"":" & Chr(34) & Token & Chr(34) & ",""call_type"":" & Chr(34) & CT & Chr(34) & ",""price"":""0""}"
    
            myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count)
                myResp = myReq.GetResponse
                myReader = New System.IO.StreamReader(myResp.GetResponseStream)
                Debug.Print("Output: " & myReader.ReadToEnd)
                Return True
            Catch ex As Exception
                Return False
            End Try
    I am calling an API and passing values and it is working fine. However if I call the above code again (without closing program) it gets stuck in the link which is in Bold. [myReq.GetRequestStream.Write]

    Why is it getting stuck or is there another way to call the API ?

    Thank you,

    GR

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,837

    Re: Calling REST API Help

    Microsoft is recommending switching to HTTPClient, which is one option. It may not solve your problem, as it might just exchange it for a different problem, but it's a different approach which you might consider. Moving from one to the other is pretty simple, so you can decide whether to try that. It might not be warranted if you are sticking with FW 4.0, as Async operations weren't quite as fully formed until FW 4.5.

    Otherwise, I'd say that you aren't closing the stream correctly, or at all. I've moved everything to HTTPClient, so I don't think I have any examples lying around for WebRequest, but consider this example:

    https://learn.microsoft.com/en-us/do...tframework-4.0

    You might find it easier to get the Stream from the Request into a variable and do the write as a separate line, or you might try just adding:

    myReq.GetRequestStream.Close()

    after you write.
    My usual boring signature: Nothing

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,100

    Re: Calling REST API Help

    You need to make sure that you dispose all the objects that support it. All the disposable objects there should be created with Using statements, so they are disposed when you're done with them. This part is definitely an issue:
    Code:
    myReader = New System.IO.StreamReader(myResp.GetResponseStream)
    both the StreamReader and the underlying Stream should be disposed. Don't declare variables in one place and then use them much later. Declare them where they're needed. Unless you specifically need to do otherwise, let the Using statement serve as the declaration, e.g.
    Code:
    Using stream = myResp.GetResponseStream(),
          reader As New StreamReader(stream)
        Debug.Print("Output: " & reader.ReadToEnd())
    End Using
    That will create the objects, use them and then dispose them. You may find that your HttpWebRequest and/or HttpWebResponse objects are disposable too, but I'm not sure. ALWAYS dispose objects that support it, i.e. that implement the IDisposable interface, and always create them with a Using statement if they are used only in the scope in which they are created.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    India
    Posts
    526

    Re: Calling REST API Help

    Thank you for your replies. I updated my code and do you think this will help.


    Code:
    Try
                Dim myReq As HttpWebRequest
                Dim myResp As HttpWebResponse
                Dim myReader As StreamReader
                Dim MMAPIURL As String = "https://restapi.mm.com/signal", Token as string = "123456678", CT as string ="BUY EXIT"
                myReq = HttpWebRequest.Create(MMAPIURL)
                myReq.Method = "POST"
                myReq.ContentType = "application/json"
                myReq.Accept = "application/json"
                Dim CT As String = Call_Type
    
                Dim myData As String = "{""token"":" & Chr(34) & Token & Chr(34) & ",""call_type"":" & Chr(34) & CT & Chr(34) & ",""price"":""0""}"
    
                myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count)
                myResp = myReq.GetResponse
                myReader = New System.IO.StreamReader(myResp.GetResponseStream)
                myReq.GetRequestStream.Close()
                myResp.Close()
                'Debug.Print("Output: " & myReader.ReadToEnd)
                myReader.Dispose()
                Return True
            Catch ex As Exception
                Return False
            End Try
    Is this statement used correctly or how to make it better (myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count) ?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,100

    Re: Calling REST API Help

    Quote Originally Posted by greatchap View Post
    do you think this will help.
    You can test it and see for yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    India
    Posts
    526

    Re: Calling REST API Help

    Thank you very much. It seems to be working fine.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,100

    Re: [RESOLVED] Calling REST API Help

    For the record, it's an improvement that you closing and/or disposing things but I would still restructure that code quite a bit. As I said, you should pretty much ALWAYS create/get an object with a Using statement if it supports disposal and will only be used in that local scope.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width