1

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?

5
  • Unless you can name a reason to use HttpWebRequest, I recommend you use WebClient (<=.NET4.0) or HttpClient (>=.NET4.5). Commented Jun 18, 2012 at 18:54
  • GET or POST depends on the PHP script. If you can open the URL in your web browser without submitting a form, it's a GET. Commented Jun 18, 2012 at 18:56
  • 1
    It is not ok to call request.BeginGetResponse(null, null);. You must call EndGetResponse. Better: use WebClient or HttpClient. Don't forget to dispose objects. Commented Jun 18, 2012 at 18:56
  • 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? Commented Jun 18, 2012 at 18:58
  • Can you provide some basic code examples using WebClient or HttpClient for my case? Commented Jun 18, 2012 at 19:02

1 Answer 1

1

Q: Do I need to make a GET or POST request?

A: This example effectively is a "GET" request. Here, I'd use "GET". If you had a form, I'd instead recommend "POST"

Q: I'm making this http-call very frequently (10-20 times in a sec); I have some speed issues.

If you happen to have the luxury of using .Net 4.x, I'd strongly recommended looking at their "asynchronous APIs":

Sign up to request clarification or add additional context in comments.

3 Comments

I thought BeginGetResponse() is an asynchronous call.
It is. But really, you're much better off using a .Net 4.x "HttpClient", as discussed in the above links. IMHO...
Can you provide some basic code examples using HttpClient for my case then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.