代码不完整(它没有构建),因为我无法在WriteResponseData()的下一行中获得“item”。
HttpWebResponse WebResp = (HttpWebResponse)item.EndGetResponse(result);
我一定错过了什么。请指出。
下面是完整的代码: -
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);
}
修改
因此,该方法的修改代码(将编译)将遵循: -
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);
}
它给出了以下运行时错误: -
对象引用未设置为 对象的实例。在 WebRequestUsingAPM.Program.WriteResponseData(IAsyncResult的 造成 \ WebRequestUsingAPM \ Program.cs:第73行 在 System.Net.LazyAsyncResult.Complete(IntPtr的 userToken) System.Net.ContextAwareResult.CompleteCallback(对象 国家) System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback 回调,对象状态,布尔值 ignoreSyncCtx)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback 回调,对象状态)at System.Net.ContextAwareResult.Complete(IntPtr的 userToken) System.Net.LazyAsyncResult.ProtectedInvokeCallback(对象 结果,IntPtr userToken)at System.Net.HttpWebRequest.ProcessResponse()
编辑: - 我知道了。 result.AsyncState返回null,因为我在调用BeginGetResponse()时传递null,第二个参数是AsyncState,我在那里传递null。我应该在那里传递当前的WebRequest对象,那样做。
所以,而不是遵循: -
item.BeginGetResponse
(
WriteResponseData, null
);
我应该通过这个项目: -
item.BeginGetResponse
(
WriteResponseData, item
);
答案 0 :(得分:1)
正如您所说,这段代码不会编译,因为WriteResponseData方法不会引用item对象。您可以在调用begin请求时使用传递请求对象作为状态。请参阅MSDN中的this code example。
编辑:简化代码模板
item.BeginGetResponse(WriteResponseData, item);
将WriteResponseData修改为
static void WriteResponseData(IAsyncResult result)
{
WebRequest request = (WebRequest)result.AsyncState;
WebResponse response = request.EndGetResponse(result);
....
}