An HTTP request from a C # program that does not require a response

I did some socket work where I had to send a request because I need an answer, but I need to write something in C # that is just going to call an old web page that needs to be answered for 10 seconds, and don't wait for an answer ( errors will be marked by DB calls).

Is there an easy way to do this?

+4
source share
6 answers

Try this topic: Async HttpWebRequest without waiting from a web application

(This approach is sometimes called "fire and forget")

+3
source

You can use Async methods in the System.Net.WebClient class:

var webClient = new System.Net.WebClient(); webClient.DownloadStringAsync("your_url") 
+5
source
+1
source

You mean something like:

 HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url); HttpWebResponse resp = (HttpWebResponse) req.GetResponse(); resp.Close(); 
-1
source

Use the Webrequest class, but run the request asynchronously . In fact, this request is executed in another thread, which you can also do yourself.

-1
source

If you want to add parameters via POST, you can also use this (and just ignore the answer if you don't need one). It takes parameters in the form of a dictionary, but can be easily changed to work in any way.

 private String DownloadData(String URL, Dictionary<String, String> Parameters) { String postString = String.Empty; foreach (KeyValuePair<string, string> postValue in Parameters) { foreach (char c in postValue.Value) { postString += String.Format("{0}={1}&", postValue.Key, postValue.Value); } } postString = postString.TrimEnd('&'); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URL); webRequest.Method = "POST"; webRequest.ContentLength = postString.Length; webRequest.ContentType = "application/x-www-form-urlencoded"; StreamWriter streamWriter = null; streamWriter = new StreamWriter(webRequest.GetRequestStream()); streamWriter.Write(postString); streamWriter.Close(); String postResponse; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); using (StreamReader responseStream = new StreamReader(webResponse.GetResponseStream())) { postResponse = responseStream.ReadToEnd(); responseStream.Close(); } return postResponse; } 
-1
source

All Articles