-
May 28th, 2024, 07:47 AM
#1
Thread Starter
Fanatic Member
[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
-
May 28th, 2024, 08:49 AM
#2
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
 
-
May 28th, 2024, 08:51 AM
#3
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.
-
May 28th, 2024, 11:19 AM
#4
Thread Starter
Fanatic Member
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) ?
-
May 28th, 2024, 11:28 AM
#5
Re: Calling REST API Help
 Originally Posted by greatchap
do you think this will help.
You can test it and see for yourself.
-
May 28th, 2024, 11:55 PM
#6
Thread Starter
Fanatic Member
Re: Calling REST API Help
Thank you very much. It seems to be working fine.
-
May 29th, 2024, 01:34 AM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|