HttpWebrequest调用顺序或未处理的异常
本文关键字:异常 未处理 调用 顺序 HttpWebrequest | 更新日期: 2023-09-27 18:14:19
我想检索网页的内容,解析它们并检索链接页面,以便解析。
我创建了一个下载方法来区分站点类型:
void startReq(string url, int type)
{
switch (type)
{
case 1:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.BeginGetResponse(new AsyncCallback(GetRequestStreamCallbackPageTypeOne), webRequest);
break;
case 2:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webReques.BeginGetResponse(new AsyncCallback(GetRequestStreamCallbackPageTypeTwo), webReques);
break;
default:
MessageBox.Show("should not get here: startreq-Method ,default-case");
break;
}
}
这里是回调:
void GetRequestStreamCallbackPageTypeOne(IAsyncResult asynchronousResult)
{
string responseString = "";
WebResponse response = ((HttpWebRequest)asynchronousResult.AsyncState).EndGetResponse(asynchronousResult);
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseString = reader.ReadToEnd();
reader.Close();
}
Dispatcher.BeginInvoke(()=>parseFirstpage(responseString));
}
可以看到,我使用Dispatcher
调用解析方法(我不知道还能怎么做)。
完成解析后,使用页类型2参数调用startReq
。但是在GetRequestStreamCallbackPageTypeTwo
方法中,其中*看起来与GetRequestStreamCallbackPageTypeOne
相同,我在这行上得到一个未处理的异常错误:
WebResponse response = ((HttpWebRequest)asynchronousResult.AsyncState).EndGetResponse(asynchronousResult);
我不明白为什么。我该如何纠正这个问题,或者有比我更好的解决方案吗?
当我运行您的项目时,我在第二个请求中获得的URL是:
http://fahrplan.avv.de/master.php?&script=inquiry/service&apikey=avvtext&script=inquiry%2Fservice&apikey=avvtext&path=%2Fhome%2FconnectionInterstations&dep_city=aachen&dep_station=uniklinik&arr_city=j%C3%BClich+&arr_station=bf&time_deparr=dep&time_h=16&time_m=10&date_d=15&date_m=12&date_y=2011&AVV_DEP=a%3A2%3A%7Bs%3A32%3A%5C%5C%5C%5C%5C%5C%5C%22bc7f0461a0b4a09f77522616469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%5C%5C%5C%5C%5C%22%3Bs%3A2%3A%5C%5C%5C%5C%5C%5C%5C%22bf%5C%5C%5C%5C%5C%5C%5C%22%3Bs%3A8%3A%5C%5C%5C%5C%5C%5C%5C%22city_gkz%5C%5C%5C%5C%5C%5C%5C%22%3BN%3Bs%3A12%3A%5C%5C%5C%5C%5C%5C%5C%22station_type%5C%5C%5C%5C%5C%5C%5C%22%3BN%3Bs%3A10%3A%5C%5C%5C%5C%5C%5C%5C%22station_id%5C%5C%5C%5C%5C%5C%5C%22%3BN%3B%7Ds%3A4%3A%5C%5C%5C%5C%5C%5C%5C%22last%5C%5C%5C%5C%5C%5C%5C%22%3Bi%3A1321202548%3Bs%3A5%3A%5C%5C%5C%5C%5C%5C%5C%22count%5C%5C%5C%5C%5C%5C%5C%22%3Bi%3A1%3B%7D%7D&showtimeframe=60&dateday=13&datemonth=11.2011&time=16%3A10&opt_timeopt=true&opt_minchange=3&opt_lowfloor=0&opt_ondemand=0&opt_rail=0&opt_surcharge=no&walkspeed=78&opt_walkpref=0&opt_nearby=false&via_pause=20&connection=NDA4NjA7OzM7MjcwNDE0MTU4MTgxNTgwODA7LTR8MyBBfDMwfDMgQXw2MHwzIEF8OTB8MyBBfDEyMHw7OTkwODc5OTQzMzc2NDg2NDQ7LTV8UkIyMHwxMXxSRTl8Mjd8UkUxfDYwfFJCMjB8NzF8UkU5fDg3fDs5OTM0MzA4NDUyNDk1NzY5NjstMnxSQjIxYXw2MHxSQjIxYXwxMjB8OzswOzA7MDs1OzA7MzswOzA7&searchConfig=80509129de625db319a7af6ef7a714c6¤tConnections=0062892b87364b4f252966c30e1eac19&connection[0]=1
这对于浏览器来说太长了,但我认为编码可能是问题所在(在AVV_DEP之后)。
我在网站上运行了一个简单的查询,并将URL硬编码到您的项目中。请求成功了。仔细查看由第一个方法返回的URL——注意URL中的编码。
有效的URL是:
http://fahrplan.avv.de/master.php?script=inquiry%2Fservice&apikey=avv&path=%2Fhome%2Fconnection&dep_city=Aachen&dep_station=&dep_city_gkz=5334002%3A0&dep_station_type=&dep_station_id=&via_city=&via_station=&via_pause=5&via_city_gkz=&via_station_type=&via_station_id=&arr_city=D%C3%BCren+%28Rheinland%29&arr_station=&arr_city_gkz=5358008%3A0&arr_station_type=&arr_station_id=&time_deparr=dep&time=13%3A30&dateday=14&datemonth=11.2011&date=14.11.2011&showtimetype=tours&showtours=5&showtimeframe=60&opt_nearby=false&walkspeed=83&opt_lowfloor=0&opt_surcharge=no&opt_ondemand=0&delfi_search=0