Search the Community
Showing results for tags 'networking'.
Found 1 result
-
Httpclient asynchronous callback and Httpclient.endAsyncHttp
pavlos posted a topic in Network, Cloud and Web
Hi everyone, I am new to Delphi and object pascal in general but I am coming from a web application background with Javascript. At work i want to implement a small unit that i will be using for any interactions with resources over the network I decided to go for HttpClient. One of the tasks i had to do first was find out how i can perform requests asynchronously. Due to the fact of docwiki.Embacardero has been down for 2 weeks now i scraped the web and had a look at the unit files in System.Net.HttpClient, System.Classes and System.types . I found two nice stackoverflow articles that help me give some direction: https://stackoverflow.com/questions/69298844/how-to-use-asynchronous-tnethttpclient https://stackoverflow.com/questions/54846698/delphi-fmx-httpclient-asynchronous-post-works-in-windows-fails-in-android Finally I managed to asynchronously make a get request uses System.Net.HttpClient, System.Net.URLCLient, System.Classes, System.Types; types asyncResponse = System.Types.IAsyncResult; var client: System.Net.HttpClient.THttpClient; asyncCallback: System.Classes.TAsyncCallback; begin client := THttpClient.Create; asyncCallback := procedure(const response: asyncResponse) begin // do something with response writeln(response.contentAsString); end; client.BeginGet(asyncCallback, 'path/to/resource'); end. However the second stackoverflow article above uses another approach: HTTPResult:= HTTPClient.BeginPost(DoEndPost,edtURL.Text,Params); procedure TMainForm.DoEndPost(const AsyncResult: IAsyncResult); begin try HTTPResponse := THTTPClient.EndAsyncHTTP(AsyncResult); TThread.Synchronize(nil, procedure begin // handle result lblStatusCode.Text := HTTPResponse.StatusCode.ToString; mmoResult.Text := HTTPResponse.ContentAsString(TEncoding.UTF8); end); finally end; end; So on the above code snippet there are 2 things that make me wonder if I am not doing something right. ( I would have looked for suggestions in docwiki but unfortunately i cant) Now i know that he is using method pointer callbacks and not anonymous functions so that might be the difference. The lines that bother me are: HTTPResponse := THTTPClient.EndAsyncHTTP(AsyncResult); Should i be also invoking this method within my callback? And the second thing that is bothering me is the fact that he is using threads. Should i also be synchronizing threads? For anyone who has made it this far, Thank you very much for taking the time. Sincerely, Pavlos- 9 replies
-
- networking
- httpclient
-
(and 1 more)
Tagged with: