webRequest 异步#region webRequest 异步 publicstatic ManualResetEvent allDone =new ManualResetEvent(false); constint BUFFER_SIZE =1024; constint DefaultTimeout =5*60*1000; // 5 minutes timeout constint DefaultRequestTimeout =4*60*1000;// 4 minutes // Abort the request if the timer fires. privatestaticvoid TimeoutCallback(object state, bool timedOut) ...{ if (timedOut) ...{ HttpWebRequest request = state as HttpWebRequest; if (request !=null) ...{ //trace Trace.Message("TimeOut : "+ request.Address.ToString()); request.Abort(); } } } publicstaticvoid createHtmlByWebRequest(string oldFile, string newFile, string url,string encoding) ...{ // Create a HttpWebrequest object to the desired URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); myHttpWebRequest.Timeout = DefaultRequestTimeout; if use proxy#region if use proxy /**//** * If you are behind a firewall and you do not have your browser proxy setup * you need to use the following proxy creation code. // Create a proxy object. WebProxy myProxy = new WebProxy(); // Associate a new Uri object to the _wProxy object, using the proxy address // selected by the user. myProxy.Address = new Uri("http://myproxy"); // Finally, initialize the Web request object proxy property with the _wProxy // object. myHttpWebRequest.Proxy=myProxy; ***/ #endregion // Create an instance of the RequestState and assign the previous myHttpWebRequest // object to its request field. RequestState myRequestState =new RequestState(); myRequestState.request = myHttpWebRequest; myRequestState.url = url; myRequestState.scFile = newFile; myRequestState.dsFile = oldFile; myRequestState.encoding = encoding; // Start the asynchronous request. IAsyncResult result = (IAsyncResult)myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback), myRequestState); // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myHttpWebRequest, DefaultTimeout, true); // The response came in the allowed time. The work processing will happen in the // callback function. allDone.WaitOne(); // Release the HttpWebResponse resource. if(myRequestState.response !=null) myRequestState.response.Close(); } privatestaticvoid RespCallback(IAsyncResult asynchronousResult) ...{ try ...{ // State of request is asynchronous. RequestState myRequestState = (RequestState)asynchronousResult.AsyncState; HttpWebRequest myHttpWebRequest = myRequestState.request; myRequestState.response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(asynchronousResult); // Read the response into a Stream object. Stream responseStream = myRequestState.response.GetResponseStream(); myRequestState.streamResponse = responseStream; // Begin the Reading of the contents of the HTML page and print it to the console. IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState); return; } catch (WebException e) ...{ Trace.Error(string.Format(" RespCallback Exception raised!")); Trace.Error(string.Format(" Message:{0}", e.Message)); Trace.Error(string.Format(" Status:{0}", e.Status)); } catch (Exception e1) ...{ Trace.Error(e1); } allDone.Set(); } privatestaticvoid ReadCallBack(IAsyncResult asyncResult) ...{ try ...{ RequestState myRequestState = (RequestState)asyncResult.AsyncState; Stream responseStream = myRequestState.streamResponse; int read = responseStream.EndRead(asyncResult); // Read the HTML page and then print it to the console. if (read >0) ...{ myRequestState.requestData.Append(Encoding.GetEncoding(myRequestState.encoding).GetString(myRequestState.BufferRead, 0, read)); IAsyncResult asynchronousResult = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState); return; } else ...{ //输出文件 if (myRequestState.requestData.Length >1) ...{ using (TextWriter streamWriter =new StreamWriter(myRequestState.scFile,false, Encoding.GetEncoding(myRequestState.encoding),1024)) ...{ streamWriter.Write(myRequestState.requestData.ToString()); streamWriter.Close(); } //update bool retry = updateHtml(myRequestState.dsFile, myRequestState.scFile, myRequestState.url); if (!retry) ...{ //重试成功 Trace.Message("重新生成页面成功:"+ myRequestState.url); } } responseStream.Close(); } } catch (WebException e) ...{ Trace.Error(string.Format(" ReadCallBack Exception raised!")); Trace.Error(string.Format(" Message:{0}", e.Message)); Trace.Error(string.Format(" Status:{0}", e.Status)); } catch (Exception e1) ...{ Trace.Error(e1); } allDone.Set(); } #endregion