Asynchronous WebRequests using C#

别来无恙 提交于 2019-12-06 02:13:44

问题


Hi i have a function that passes url Get parameters to a php file on a webserver and waits for a response from the file (normally takes 10-20 seconds). I want to put this inside a loop because I have to send these Get requests to about 5 different php files at once but when i try to add it to a loop the function makes the loop wait until the file returns the response before it will go on to the next one.

    public string HttpGet(string URI, string Parameters)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URI + Parameters);

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader resStream = new StreamReader(response.GetResponseStream());
          return resStream.ReadToEnd().Trim();
    }

    private void SendCommand()
    {
        for( int i = 0; i <= 4; i++)
        {
            AddRTB(HttpGet(url, paramater));
        }
    }

Is there a way that i could send the all 5 requests at once without waiting for the previous to finish? (i was thinking about threading it but alas i never touched it before is i don't know where to start.)


回答1:


Instead of using the GetResponse() method you could use the BeginGetResponse() which is a non-blocking call. It takes a callback that can then handle the WebResponse object when it finally returns. The example in the link will give you a good idea of how to have the main thread wait for all the responses to return.




回答2:


Here are two approaches which uses the TPL.

The first waits for all the requests to complete before you access any of the results

var runningTasks = new List<Task<string>>();

for (int ii = 0; ii <= 4; ii++)
{
    var wreq = (HttpWebRequest)WebRequest.Create("..." + ii);

    var taskResp = Task.Factory.FromAsync<WebResponse>(wreq.BeginGetResponse, 
                                                    wreq.EndGetResponse, 
                                                    null);
    var taskResult = taskResp.ContinueWith(tsk => new StreamReader(tsk.Result.GetResponseStream()).ReadToEnd().Trim());
    runningTasks.Add(taskResult);
}

Task.WaitAll(runningTasks.ToArray());
IEnumerable<string> results = runningTasks.Select(tsk => tsk.Result);

and the second does something with each result as it comes in:

for (int ii = 0; ii <= 4; ii++)
{
    var wreq = (HttpWebRequest)WebRequest.Create("..." + ii);

    var taskResp = Task.Factory.FromAsync<WebResponse>(wreq.BeginGetResponse, 
                                                    wreq.EndGetResponse, 
                                                    null);
    taskResp.ContinueWith(tsk => new StreamReader(tsk.Result.GetResponseStream()).ReadToEnd().Trim())
            .ContinueWith((Task<string> trs) => 
                { 
                    var result = trs.Result;
                    DoSomthingWithTheResult(result);
                });
}



回答3:


Instead of GetResponse use BeginGetResonse method of request class. Sample and documentation can be found at http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx




回答4:


Use WebClient with Async methods.

Begin\End is more difficult to use.



来源:https://stackoverflow.com/questions/3799242/asynchronous-webrequests-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!