WebRequest- retrieving data asynchronously for multiple requests
The code is not complete (It does not build), as I am not able to get the "item" in the following line in the WriteResponseData().
HttpWebResponse WebResp = (HttpWebResponse)item.EndGetResponse(result);
I must be missing something. Please point out.
below is the complete code:-
static string[] imageUris =
{ "http://www.cricinfo.com/db/PICTURES/CMS/118200/118217.2.jpg",
"http://www.cricinfo.com/db/PICTURES/CMS/117500/117599.2.jpg",
"http://www.cricinfo.com/db/PICTURES/CMS/117500/117598.2.jpg",
"http://www.cricinfo.com/db/PICTURES/CMS/106400/106412.2.jpg",
"http://www.cricinfo.com/db/PICTURES/CMS/106400/106411.2.jpg",
"http://www.cricinfo.com/db/PICTURES/CMS/106200/106275.2.jpg",
"http://www.cricinfo.com/db/PICTURES/CMS/106300/106362.2.jpg"
};
static void Main(string[] args)
{
SendAsynchronousRequests();
Console.WriteLine("All the files have been requested and retrieved...");
Console.ReadLine();
}
static void SendAsynchronousRequests()
{
WebRequest[] requests = InitializeWebRequests();
foreach (var item in requests)
{
Console.WriteLine("Trying to retrieve :-" + item.RequestUri.OriginalString);
item.BeginGetResponse
(
WriteResponseData,
);
}
}
static WebRequest[] InitializeWebRequests()
{
WebRequest[] requests = new WebRequest[imageUris.Length];
int temp = 0;
foreach (var item in imageUris)
{
requests[temp] = WebRequest.Create(item);
//requests[temp].Method = "GET";
temp++;
}
return requests;
}
static void WriteResponseData(IAsyncResult result)
{
HttpWebResponse WebResp = (HttpWebResponse)item.EndGetResponse(result);
Console.WriteLine("WebResp.StatusCode :-" + WebResp.StatusCode);
Console.WriteLine("WebResp.Server :- " + WebResp.Server);
Console.WriteLine("WebResp.ContentLength :- " + WebResp.ContentLength.ToString());
Console.WriteLine(Environment.NewLine);
}
EDIT
So, the modified code for the method (which would compile) would be following :-
WebRequest request = (WebRequest)result.AsyncState;
if (result != null && result.IsCompleted)
{
HttpWebResponse WebResp = (HttpWebResponse)request.EndGetResponse(result);
Console.WriteLine("WebResp.StatusCode :-" + WebResp.StatusCode);
Console.WriteLine("WebResp.Server :- " + WebResp.Server);
Console.WriteLine("WebResp.ContentLength :- " + WebResp.ContentLength.ToString());
Console.WriteLine(Environment.NewLine);
}
It gives me following run-time error:-
Object reference not set to an instance of an object. at WebRequestUsingAPM.Program.WriteResponseData(IAsyncResult result) in \WebRequestUsingAPM\Program.cs:line 73 at System.Net.LazyAsyncResult.Complete(IntPtr userToken) at System.Net.ContextAwareResult.CompleteCallback(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.ContextAwareResult.Complete(IntPtr userToken) at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken) at System.Net.HttpWebRequest.ProcessResponse()
EDIT :- I get it. result.AsyncState returns null because I pass null when I am calling the BeginGetResponse(), the second argument is the AsyncState and I am passing null there. I should pass the current WebRequest object there and that would do.
So, instead of following :-
item.BeginGetResponse
(
WriteResponseData, null
);
I should pass the item:-
item.BeginGetResponse
开发者_如何学JAVA (
WriteResponseData, item
);
As you have said, this code would't compile simply because WriteResponseData method will not have reference to item object. You can use pass your request object as the state while calling begin request. See this code example from MSDN.
Edit : a simplified code template would be
item.BeginGetResponse(WriteResponseData, item);
And modify WriteResponseData as
static void WriteResponseData(IAsyncResult result)
{
WebRequest request = (WebRequest)result.AsyncState;
WebResponse response = request.EndGetResponse(result);
....
}
精彩评论