public void Download(string url, UInt32 requestId) { //判断文件是否存在 string fileName = m_saveFolder + url.Substring(url.LastIndexOf("/") + 1); if (File.Exists(fileName)) { Debug.WriteLine("File already download " + fileName); DownloadEventArgs args = new DownloadEventArgs(); args.requestId = requestId; args.result = fileName; DownloadEvent(this, args); return; } try { HttpWebRequest req = CreateRequest(url, "GET"); WebAsyncObject webObject = new WebAsyncObject(); webObject.request = req; webObject.requestId = requestId; webObject.webClient = this; req.BeginGetResponse(new AsyncCallback(onDownloadResponse), webObject); } catch (Exception exception) { Debug.WriteLine(exception.Message); DownloadEventArgs args = new DownloadEventArgs(); args.requestId = requestId; args.result = ""; DownloadEvent(this, args); } }
static void OnResponse(IAsyncResult ar) { //Debug.WriteLine("OnResponse 1:" + System.Threading.Thread.CurrentThread.ManagedThreadId); var webObject = (WebAsyncObject)ar.AsyncState; var request = webObject.request; var requestId = webObject.requestId; var webClient = webObject.webClient; HttpWebResponse response = null; Stream stream = null; StreamReader sr = null; string result = ""; try { response = (HttpWebResponse)request.EndGetResponse(ar); stream = response.GetResponseStream(); sr = new StreamReader(stream, Encoding.UTF8); result = sr.ReadToEnd(); } catch (Exception exception) { Debug.WriteLine(exception.Message); } finally { if (sr != null) { sr.Close(); } if (stream != null) { stream.Close(); } if (response != null) { response.Close(); } request.Abort(); } //Debug.WriteLine("OnResponse 2:" + System.Threading.Thread.CurrentThread.ManagedThreadId); if (webClient.DownloadEvent != null) { DownloadEventArgs args = new DownloadEventArgs(); args.requestId = requestId; args.result = result; webClient.DownloadEvent(webClient, args); } }
public void Get(string url, UInt32 requestId) { //Debug.WriteLine("Get " + System.Threading.Thread.CurrentThread.ManagedThreadId); try { HttpWebRequest req = CreateRequest(url, "GET"); WebAsyncObject webObject = new WebAsyncObject(); webObject.request = req; webObject.requestId = requestId; webObject.webClient = this; req.BeginGetResponse(new AsyncCallback(OnResponse), webObject); } catch (Exception exception) { Debug.WriteLine(exception.Message); DownloadEventArgs args = new DownloadEventArgs(); args.requestId = requestId; args.result = ""; DownloadEvent(this, args); } }
public void Post(string url, string postData, UInt32 requestId) { //Debug.WriteLine("Post " + System.Threading.Thread.CurrentThread.ManagedThreadId); try { HttpWebRequest req = CreateRequest(url, "POST"); byte[] bs = Encoding.ASCII.GetBytes(postData); req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = bs.Length; if (referer.Length > 0) { req.Referer = referer; } req.Accept = "*/*"; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); reqStream.Close(); } WebAsyncObject webObject = new WebAsyncObject(); webObject.request = req; webObject.requestId = requestId; webObject.webClient = this; req.BeginGetResponse(new AsyncCallback(OnResponse), webObject); } catch (Exception exception) { Debug.WriteLine(exception.Message); DownloadEventArgs args = new DownloadEventArgs(); args.requestId = requestId; args.result = ""; DownloadEvent(this, args); } }
private void onDownloadEvent(object sender, DownloadEventArgs e) { onDownload(e);//切换线程 }
private void onDownload(DownloadEventArgs e) { // InvokeRequired需要比较调用线程ID和创建线程ID // 如果它们不相同则返回true if (m_invokeObj.InvokeRequired) { Debug.WriteLine("onDownload 1:" + System.Threading.Thread.CurrentThread.ManagedThreadId); var d = new OnDownloadCallback(onDownload); m_invokeObj.Invoke(d, new object[] { e }); } else { Debug.WriteLine("onDownload 2:" + System.Threading.Thread.CurrentThread.ManagedThreadId); var result = e.result; var requestId = e.requestId; var searchInfo = getSearchType(requestId); if (searchInfo == null) { Debug.WriteLine("Warning: requst not exist id:" + requestId + " result:" + result); return; } var searchType = searchInfo.searchType; var queueId = searchInfo.queueId; Debug.WriteLine("request id:" + requestId + " que:" + queueId + " result:" + result + " searchType" + searchType); Song song = null; string songPath = ""; switch (searchType) { case SearchType.Invalid: break; case SearchType.SongList: var songList = JsonHelper.getSongList(result); if (songList.Count > 0) { song = songList[0]; //只下载第一首歌 } break; case SearchType.SongInfo: song = JsonHelper.getSong(result); if (song != null) { var quality = song.Quality; if (quality == null) { song = null; } } break; case SearchType.SongDownload: songPath = result; break; default: break; } var resultInfo = new SearchResult(); resultInfo.SongInfo = song; resultInfo.QueueId = queueId; resultInfo.SongPath = songPath; if (SearchResultEvent != null) { SearchResultEvent(resultInfo); } } }
static void onDownloadResponse(IAsyncResult ar) { var webObject = (WebAsyncObject)ar.AsyncState; var request = webObject.request; var requestId = webObject.requestId; var webClient = webObject.webClient; HttpWebResponse response = null; Stream stream = null; FileStream fs = null; var url = request.RequestUri; var urlStr = url.AbsoluteUri; string fileName = webClient.m_saveFolder + urlStr.Substring(urlStr.LastIndexOf("/") + 1); string tmpFileName = fileName + webClient.m_tmpFileExtName; bool isError = false; if (File.Exists(fileName)) { Debug.WriteLine("Error: file already exist:" + fileName); isError = true; } if (File.Exists(tmpFileName)) { Debug.WriteLine("Error: tmp file already exist:" + tmpFileName); isError = true; } try { if (!isError) { response = (HttpWebResponse)request.EndGetResponse(ar); stream = response.GetResponseStream(); byte[] buffer = new byte[32 * 1024]; int bytesProcessed = 0; fs = File.Create(tmpFileName); int bytesRead; do { bytesRead = stream.Read(buffer, 0, buffer.Length); fs.Write(buffer, 0, bytesRead); bytesProcessed += bytesRead; }while (bytesRead > 0); } } catch (Exception exception) { Debug.WriteLine(exception.Message); isError = true; } finally { if (fs != null) { fs.Flush(); fs.Close(); } if (stream != null) { stream.Close(); } if (response != null) { response.Close(); } request.Abort(); if (isError) //出错了 { //删除文件 if (File.Exists(tmpFileName)) { File.Delete(tmpFileName); } fileName = ""; } else { if (File.Exists(tmpFileName)) { File.Move(tmpFileName, fileName); } else { fileName = ""; } } } if (webClient.DownloadEvent != null) { DownloadEventArgs args = new DownloadEventArgs(); args.requestId = requestId; args.result = fileName; webClient.DownloadEvent(webClient, args); } }