public static void DownloadFile(string urlName, string localName) { try { Uri uri = new Uri(urlName); WebRequestExpress request = null; request = WebRequestExpress.Create(uri, request, true); request.Header = request.Method + " " + uri.PathAndQuery + " HTTP/1.0\r\n" + request.Headers; WebResponseExpress response = request.GetResponse(); response.EncodingType = Encoding.UTF8; byte[] RecvBuffer = new byte[1024]; int nBytes, nTotalBytes = 0; // loop to receive response buffer while ((nBytes = response.socket.Receive(RecvBuffer, 0, 1024, SocketFlags.None)) > 0) { nTotalBytes += nBytes; // save block to end of file SaveToFile(RecvBuffer, nBytes, localName); if (response.KeepAlive && nTotalBytes >= response.ContentLength && response.ContentLength > 0) { break; } } } catch (Exception ex) { throw ex; } }
public WebResponseExpress GetResponse() { if (response == null || response.socket == null || response.socket.Connected == false) { response = new WebResponseExpress(); response.Connect(this); response.SetTimeout(Timeout); } response.SendRequest(this); response.ReceiveHeader(); return(response); }
public static string DownloadBySocket(string url, Encoding encodingType) { if (Utility.Utility.IsStringNullOrEmpty(url)) { return(Utility.Utility.InitializeString); } Uri uri = new Uri(url); WebRequestExpress request = null; request = WebRequestExpress.Create(uri, request, true); request.Header = request.Method + " " + uri.PathAndQuery + " HTTP/1.0\r\n" + request.Headers; WebResponseExpress response = request.GetResponse(); if (encodingType == null) { encodingType = Encoding.UTF8; } response.EncodingType = encodingType; string strResponse = ""; byte[] RecvBuffer = new byte[10240]; int nBytes, nTotalBytes = 0; // loop to receive response buffer while ((nBytes = response.socket.Receive(RecvBuffer, 0, 10240, SocketFlags.None)) > 0) { nTotalBytes += nBytes; strResponse += encodingType.GetString(RecvBuffer, 0, nBytes); if (response.KeepAlive && nTotalBytes >= response.ContentLength && response.ContentLength > 0) { break; } } return(strResponse); }