I need to call a url like the following, programmatically, using C#:
http://mysite.com/AdjustSound.php
This php file expects a SoundLevel from me. So, an example call would be something like that:
http://mysite.com/AdjustSound.php?SoundLevel=30
I have 2 questions:
1:
WebRequest request =
WebRequest.Create("http://mysite.com/AdjustSound.php?SoundLevel=30");
// Which one?
// request.Method = "GET";
// request.Method = "POST";
Question 1: Do I need to make a GET or POST request?
2:
Since, I'm making this http-call very frequently (10-20 times in a sec); I have some speed issues. So, I don't want my program to wait till this http call finishes and retrieves the result. I want that Webrequest to run asynchronously.
The other issue is that I don't need to see the results of this http call. I just want to invoke the server side. And even, I don't care if this call finished successfully or not... (If it fails, most probably I will adjust the sound a few milliseconds later. So, I don't care.) I wrote the following code:
WebRequest request =
WebRequest.Create("http://mysite.com/AdjustSound.php?SoundLevel=30");
request.Method = "GET";
request.BeginGetResponse(null, null);
Question 2 : Doest it seem ok to run this code? Is that ok to call request.BeginGetResponse(null, null); ?
EDIT
After reading the comments; I modified my code like the following:
WebClient webClient = new WebClient();
Uri temp = new Uri("http://mysite.com/AdjustSound.php?SoundLevel=30");
webClient.UploadStringAsync(temp, "GET", "");
Is that ok/better now?
request.BeginGetResponse(null, null);. You must call EndGetResponse. Better: use WebClient or HttpClient. Don't forget to dispose objects.I'm making this http-call very frequently (10-20 times in a sec)- are you sure HTTP is the right way to do this? Is the HTTP server local?WebClientorHttpClientfor my case?