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 void Connect(WebRequestExpress request)
        {
            ResponseUri = request.RequestUri;

            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint remoteEP = new IPEndPoint(Dns.GetHostEntry(ResponseUri.Host).AddressList[0], ResponseUri.Port);

            socket.Connect(remoteEP);
        }
        public void SendRequest(WebRequestExpress request)
        {
            ResponseUri = request.RequestUri;

            request.Header = request.Method + " " + ResponseUri.PathAndQuery + " HTTP/1.0\r\n" + request.Headers;
            if (EncodingType != null)
            {
                socket.Send(EncodingType.GetBytes(request.Header));
            }
            else
            {
                socket.Send(Encoding.UTF8.GetBytes(request.Header));
            }
        }
Esempio n. 4
0
 public static WebRequestExpress Create(Uri uri, WebRequestExpress AliveRequest, bool bKeepAlive)
 {
     if (bKeepAlive &&
         AliveRequest != null &&
         AliveRequest.response != null &&
         AliveRequest.response.KeepAlive &&
         AliveRequest.response.socket.Connected &&
         AliveRequest.RequestUri.Host == uri.Host)
     {
         AliveRequest.RequestUri = uri;
         return(AliveRequest);
     }
     return(new WebRequestExpress(uri, bKeepAlive));
 }
        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);
        }