6

I've done some socket work where I had to send a request because I wanted a response back, but I need to write something in C# that's just going to call an old web page which takes about 10 seconds to respond, and not wait for the response (the failures will flag up with DB calls).

Is there a simple way to do this?

Tim Almond
  • 12,088
  • 10
  • 40
  • 50
  • Everyone seems to answer using "use async." For future readers - please do not do that in anything that requires scalability. You will hold up threads and sockets waiting for the response. The proper answer here is that no, there doesn't seem to be a built-in easy way to do this. The "lowest" level HTTP class, `HttpWebRequest`, provides no access to the underlying Socket/connection. Using `KeepAlive=false` can help somewhat. – makhdumi Oct 31 '17 at 20:56

6 Answers6

6

Try this thread: Async HttpWebRequest with no wait from within a web application

(This kind of approach is sometimes known as "fire and forget")

Community
  • 1
  • 1
Rick Liddle
  • 2,684
  • 19
  • 31
  • 1
    Using Async is still waiting for the response, just in another thread. This doesn't answer the question. Adding a separate thread that's going to be sitting for seconds or minutes is not scalable. – makhdumi Oct 31 '17 at 20:45
  • There are no threads. It's a software interrupt. – PRMan May 05 '21 at 21:38
5

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

var webClient = new System.Net.WebClient();
webClient.DownloadStringAsync("your_url")
David
  • 34,223
  • 3
  • 62
  • 80
  • This doesn't answer the question. Using Async still holds up a thread and socket waiting for the response. The proper solution here is to kill the TCP connection. – makhdumi Oct 31 '17 at 20:54
1

Try this MSDN document

Jay
  • 6,224
  • 4
  • 20
  • 23
0

Do you mean something like:

HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
resp.Close();
Chris
  • 7,229
  • 7
  • 41
  • 57
0

Use the Webrequest class, but start the request asynchronously. This essentially is running the request in another thread which you could also do on your own.

codymanix
  • 28,510
  • 21
  • 92
  • 151
0

If you want to add parameters via POST you can also use this (and simply ignore the response if you don't need it). This takes parameters in the form of a dictionary but can easily be modified to work any way you want.

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;
}
Jon
  • 150
  • 3