private static void RespCallback(IAsyncResult ar)
    {
        // State of request is asynchronous.
        RequestDeclare requestDeclare = (RequestDeclare)ar.AsyncState;

        FileWebRequest myFileWebRequest = requestDeclare.myFileWebRequest;

        // End the Asynchronus request by calling the 'EndGetResponse()' method.

        FileWebResponse myFileWebResponse = (FileWebResponse)myFileWebRequest.EndGetResponse(ar);

        // Reade the response into Stream.
        StreamReader streamReader = new StreamReader(myFileWebResponse.GetResponseStream());

        Char[] readBuffer = new Char[256];

        int count = streamReader.Read(readBuffer, 0, 256);

        Console.WriteLine("The contents of the file are :\n");

        while (count > 0)
        {
            String str = new String(readBuffer, 0, count);
            Console.WriteLine(str);
            count = streamReader.Read(readBuffer, 0, 256);
        }

        streamReader.Close();
        // Release the response object resources.
        myFileWebResponse.Close();
        allDone.Set();
        Console.WriteLine("File reading is over.");
    }
Beispiel #2
0
        public override Uri GetUserRequest(Uri input)
        {
            // Create a request for the local filepath (ex. C:/Users/joxx01812/source/repos/sqlFile.csv).
            FileWebRequest request = (FileWebRequest)FileWebRequest.Create(input);

            // Get the response.
            FileWebResponse response = (FileWebResponse)request.GetResponse();

            // Get the stream containing content returned by the server.
            // The using block ensures the stream is automatically closed.
            using (Stream dataStream = response.GetResponseStream())
            {
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);

                // Read the content.
                string responseFromUserInput = reader.ReadToEnd();

                // Display the content.
                Console.WriteLine(responseFromUserInput);

                // CleanUp streams.
                reader.Close();
                dataStream.Close();
            }
            // Close the response.
            response.Close();
            return(input);
        }
Beispiel #3
0
        private XmlDocument getUpdateFile(string updateUrl)
        {
            XmlDocument Winkle_XMLdoc = null;

            if (updateUrl.StartsWith("file", true, System.Globalization.CultureInfo.CurrentCulture))
            {
                FileWebRequest  Winkle_Request;
                FileWebResponse Winkle_Response = null;

                try
                {
                    Winkle_Request = (FileWebRequest)WebRequest.Create(string.Format(updateUrl));
                    //Winkle_Request.UserAgent = @"Winkle automatic update system " + winkleVersion;
                    Winkle_Response = (FileWebResponse)Winkle_Request.GetResponse();
                    Winkle_XMLdoc   = new XmlDocument();
                    Winkle_XMLdoc.Load(Winkle_Response.GetResponseStream());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                if (Winkle_Response != null)
                {
                    Winkle_Response.Close();
                }
            }
            else
            {
                HttpWebRequest  Winkle_Request;
                HttpWebResponse Winkle_Response = null;

                try
                {
                    Winkle_Request           = (HttpWebRequest)WebRequest.Create(string.Format(updateUrl));
                    Winkle_Request.UserAgent = @"Winkle automatic update system " + winkleVersion;
                    Winkle_Response          = (HttpWebResponse)Winkle_Request.GetResponse();
                    Winkle_XMLdoc            = new XmlDocument();
                    Winkle_XMLdoc.Load(Winkle_Response.GetResponseStream());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                if (Winkle_Response != null)
                {
                    Winkle_Response.Close();
                }
            }

            return(Winkle_XMLdoc);
        }
Beispiel #4
0
        }//end ProcessTable

        public void ProcessList(Uri url, String listType, int optionList)
        {

            //ignore bad cert code
            IgnoreBadCertificates();

            //code to allow program with work with different authentication schemes
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            String listTypeEnd = listType + "/";
            //WebRequest http = HttpWebRequest.Create(url);
            FileWebRequest http = (FileWebRequest)WebRequest.Create(url);
            //HttpWebResponse response = (HttpWebResponse)http.GetResponse();
            FileWebResponse response = (FileWebResponse)http.GetResponse();
            Stream istream = response.GetResponseStream();
            ParseHTML parse = new ParseHTML(istream);
            StringBuilder buffer = new StringBuilder();
            bool capture = false;
            Advance(parse, listType, optionList);
            int ch;
            while ((ch = parse.Read()) != -1)
            {
                if (ch == 0)
                {
                    HTMLTag tag = parse.Tag;
                    if (String.Compare(tag.Name, "li", true) == 0)
                    {
                        if (buffer.Length > 0)
                            ProcessItem(buffer.ToString());
                        buffer.Length = 0;
                        capture = true;
                    }
                    else if (String.Compare(tag.Name, "/li", true) == 0)
                    {
                        // Console.WriteLine(buffer.ToString());  //creates a double listing of each list item, might be left over debugging code
                        ProcessItem(buffer.ToString());
                        buffer.Length = 0;
                        capture = false;
                    }
                    else if (String.Compare(tag.Name, listTypeEnd, true) == 0)
                    {
                        break;
                    }
                }
                else
                {
                    if (capture)
                        buffer.Append((char)ch);
                }
            }
        }//end ProcessList
    public static void GetPage(String url)
    {
        try
        {
// <Snippet1>
            Uri fileUrl = new Uri("file://" + url);
            // Create a 'FileWebrequest' object with the specified Uri.
            FileWebRequest myFileWebRequest = (FileWebRequest)WebRequest.Create(fileUrl);
            // Send the 'FileWebRequest' object and wait for response.
            FileWebResponse myFileWebResponse = (FileWebResponse)myFileWebRequest.GetResponse();

            // Get the stream object associated with the response object.
            Stream receiveStream = myFileWebResponse.GetResponseStream();

            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            // Pipe the stream to a higher level stream reader with the required encoding format.
            StreamReader readStream = new StreamReader(receiveStream, encode);
            Console.WriteLine("\r\nResponse stream received");

            Char[] read = new Char[256];
            // Read 256 characters at a time.
            int count = readStream.Read(read, 0, 256);
            Console.WriteLine("File Data...\r\n");
            while (count > 0)
            {
                // Dump the 256 characters on a string and display the string onto the console.
                String str = new String(read, 0, count);
                Console.Write(str);
                count = readStream.Read(read, 0, 256);
            }
            Console.WriteLine("");
            // Release resources of stream object.
            readStream.Close();
            // Release resources of response object.
            myFileWebResponse.Close();
// </Snippet1>
        }
        catch (WebException e)
        {
            Console.WriteLine("\r\nWebException thrown. The Reason for failure is : {0}", e.Status);
        }
        catch (Exception e)
        {
            Console.WriteLine("\nThe following Exception was raised : {0}", e.Message);
        }
    }
Beispiel #6
0
        private static void readFile()
        {
            try
            {
                // Create the file stream.
                Stream receiveStream = myFileWebResponse.GetResponseStream();

                // Create a reader object to read the file content.
                StreamReader readStream = new StreamReader(receiveStream);

                // Create a local buffer for a temporary storage of the
                // read data.
                char[] readBuffer = new Char[256];

                // Read the first 256 bytes.
                int count = readStream.Read(readBuffer, 0, 256);

                Console.WriteLine("The file content is:");
                Console.WriteLine("");

                // Loop to read the remaining data in blocks of 256 bytes
                // and display the data on the console.
                while (count > 0)
                {
                    String str = new String(readBuffer, 0, count);
                    Console.WriteLine(str + "\n");
                    count = readStream.Read(readBuffer, 0, 256);
                }

                readStream.Close();

                // Release the response object resources.
                myFileWebResponse.Close();
            }
            catch (WebException e)
            {
                Console.WriteLine("The WebException: " + e.Message);
            }
            catch (UriFormatException e)
            {
                Console.WriteLine("The UriFormatException: " + e.Message);
            }
        }
Beispiel #7
0
        public override Stream GetResponseStream()
        {
            Stream       stream = _webResponse.GetResponseStream();
            StreamReader reader = new StreamReader(stream);

            if (reader.CurrentEncoding != Encoding.UTF8)
            {
                return(stream);
            }
            using (reader)
            {
                byte[] temp = new byte[3];
                stream.Read(temp, 0, 3);
                stream.Seek(0, SeekOrigin.Begin);
                MemoryStream memoryStream = new MemoryStream(1024);
                if (!(temp[0] == 239 && temp[1] == 187 && temp[2] == 191))
                {
                    memoryStream.Write(new byte[] { 239, 187, 191 }, 0, 3);
                }
                StreamToMemoryStream(stream, memoryStream);
                return(memoryStream);
            }
        }
Beispiel #8
0
        private string MakeFileRequest(string Url)
        {
            Uri    uri     = new Uri(Url);
            string Content = string.Empty;

            if (uri.Scheme == Uri.UriSchemeFile)
            {
                FileWebRequest req = (FileWebRequest)FileWebRequest.Create(uri);

                FileWebResponse resp = req.GetResponse() as FileWebResponse;

                Stream stream = resp.GetResponseStream();

                using (StreamReader reader = new StreamReader(stream))
                {
                    Content = reader.ReadToEnd();
                }

                resp.Close();
            }

            return(Content);
        }
        public void GetResponseStream()
        {
            FileWebRequest req = (FileWebRequest)WebRequest.Create(_tempFileUri);

            req.Method      = "PUT";
            req.ContentType = "image/png";
            using (Stream s = req.GetRequestStream()) {
                s.WriteByte(72);
                s.WriteByte(110);
                s.WriteByte(80);
                s.Flush();
            }
            req = (FileWebRequest)WebRequest.Create(_tempFileUri);
            FileWebResponse respA = null;
            FileWebResponse respB = null;
            FileStream      fsA   = null;
            FileStream      fsB   = null;

            try {
                respA = (FileWebResponse)req.GetResponse();
                fsA   = respA.GetResponseStream() as FileStream;
                Assert.IsNotNull(fsA, "#A1");
                Assert.IsTrue(fsA.CanRead, "#A2");
                Assert.IsTrue(fsA.CanSeek, "#A3");
                Assert.IsFalse(fsA.CanTimeout, "#A4");
                Assert.IsFalse(fsA.CanWrite, "#A5");
                Assert.AreEqual(3, fsA.Length, "#A6");
                Assert.AreEqual(0, fsA.Position, "#A7");
                try {
                    int i = fsA.ReadTimeout;
                    Assert.Fail("#A8:" + i);
                } catch (InvalidOperationException) {
                }
                try {
                    int i = fsA.WriteTimeout;
                    Assert.Fail("#A9:" + i);
                } catch (InvalidOperationException) {
                }

                respB = (FileWebResponse)req.GetResponse();
                fsB   = respB.GetResponseStream() as FileStream;
                Assert.IsNotNull(fsB, "#B1");
                Assert.IsTrue(fsB.CanRead, "#B2");
                Assert.IsTrue(fsB.CanSeek, "#B3");
                Assert.IsFalse(fsB.CanTimeout, "#B4");
                Assert.IsFalse(fsB.CanWrite, "#B5");
                Assert.AreEqual(3, fsB.Length, "#B6");
                Assert.AreEqual(0, fsB.Position, "#B7");
                try {
                    int i = fsB.ReadTimeout;
                    Assert.Fail("#B8:" + i);
                } catch (InvalidOperationException) {
                }
                try {
                    int i = fsB.WriteTimeout;
                    Assert.Fail("#B9:" + i);
                } catch (InvalidOperationException) {
                }
            } finally {
                if (respA != null)
                {
                    respA.Close();
                }
                if (respB != null)
                {
                    respB.Close();
                }
            }
        }
Beispiel #10
0
        /// <summary>
        /// returns a collection of bytes from a Url
        /// </summary>
        /// <param name="url">The URL to retrieve</param>
        public void GetUrlData(string url)
        {
            Uri uri = new Uri(url);

            if (!uri.IsFile)
            {
                throw new UriFormatException("url is not a local file");
            }

            FileWebRequest request = WebRequest.Create(url) as FileWebRequest;

            if (request == null)
            {
                this.Clear();
                return;
            }

            request.Method = "GET";

            // download the target URL
            FileWebResponse response = (FileWebResponse)request.GetResponse();

            // convert response stream to byte array
            using (Stream stream = response.GetResponseStream())
            {
                ExtendedBinaryReader extReader = new ExtendedBinaryReader(stream);
                _ResponseBytes = extReader.ReadToEnd();
            }

            // For local operations, we consider the data are never compressed. Else, the "Content-Encoding" field
            // in the headers would be "gzip" or "deflate". This could be handled quite easily with SharpZipLib for instance.

            // sometimes URL is indeterminate, eg, "http://website.com/myfolder"
            // in that case the folder and file resolution MUST be done on
            // the server, and returned to the client as ContentLocation
            _ContentLocation = response.Headers["Content-Location"];
            if (_ContentLocation == null)
            {
                _ContentLocation = "";
            }

            // if we have string content, determine encoding type
            // (must cast to prevent null)
            // HACK We determine the content type based on the uri extension,
            // as the header returned by the FileWebResponse is always "application/octet-stream" (hard coded in .NET!!)
            // text/html
            string ext = Path.GetExtension(uri.LocalPath).TrimStart(new char[] { '.' });

            switch (ext)
            {
            // What's important here is to identify TEXT mime types. Because, the default will resort to binary file.
            case "htm":
            case "html":    _DetectedContentType = "text/html";                     break;

            case "css":             _DetectedContentType = "text/css";                      break;

            case "csv":             _DetectedContentType = "text/csv";                      break;

            case "rtf":             _DetectedContentType = "text/rtf";                      break;

            case "aspx":
            case "xsl":
            case "xml":             _DetectedContentType = "text/xml";                      break;

            case "bmp":             _DetectedContentType = "image/bmp";                     break;

            case "gif":             _DetectedContentType = "image/gif";                     break;

            case "ico":             _DetectedContentType = "image/x-icon";          break;

            case "jpg":
            case "jpeg":    _DetectedContentType = "image/jpeg";            break;

            case "png":             _DetectedContentType = "image/png";                     break;

            case "tif":
            case "tiff":    _DetectedContentType = "image/tiff";            break;

            case "js":              _DetectedContentType = "application/x-javascript";                      break;

            default:
                // Line commented: we don't change it
                _DetectedContentType = response.Headers["Content-Type"];                                // Always "application/octet-stream" ...
                break;
            }
            if (_DetectedContentType == null)
            {
                _DetectedContentType = "";
            }
            if (ResponseIsBinary)
            {
                _DetectedEncoding = null;
            }
            else if (_ForcedEncoding == null)
            {
                _DetectedEncoding = DetectEncoding(_DetectedContentType, _ResponseBytes);
            }
        }
Beispiel #11
0
        private void DownloadFile(string FileName, string strUrl)
        {
            HttpWebRequest  webRequest;
            HttpWebResponse webResponse = null;
            FileWebRequest  fileRequest;
            FileWebResponse fileResponse = null;
            bool            isFile       = false;

            try
            {
                System.Globalization.DateTimeFormatInfo dfi = null;
                System.Globalization.CultureInfo        ci  = null;
                ci  = new System.Globalization.CultureInfo("zh-CN");
                dfi = new System.Globalization.DateTimeFormatInfo();

                //WebRequest wr = WebRequest.Create("");

                //System.Net.WebResponse w=wr.
                DateTime      fileDate;
                long          totalBytes;
                DirectoryInfo theFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
                string        fileName  = Path.Combine(theFolder.FullName, FileName);

                isFile = (HttpWebRequest.Create(strUrl) is FileWebRequest);

                if (isFile)
                {
                    fileRequest  = (FileWebRequest)FileWebRequest.Create(strUrl);
                    fileResponse = (FileWebResponse)fileRequest.GetResponse();
                    if (fileResponse == null)
                    {
                        return;
                    }
                    fileDate   = DateTime.Now;
                    totalBytes = fileResponse.ContentLength;
                }
                else
                {
                    webRequest  = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                    webResponse = (HttpWebResponse)webRequest.GetResponse();
                    if (webResponse == null)
                    {
                        return;
                    }
                    fileDate   = webResponse.LastModified;
                    totalBytes = webResponse.ContentLength;
                }

                //pbUpdate.Maximum = Convert.ToInt32(totalBytes);

                Stream stream;
                if (isFile)
                {
                    stream = fileResponse.GetResponseStream();
                }
                else
                {
                    stream = webResponse.GetResponseStream();
                }
                FileStream sw = new FileStream(fileName, FileMode.Create);
                int        totalDownloadedByte = 0;
                Byte[]     @by   = new byte[1024];
                int        osize = stream.Read(@by, 0, @by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    sw.Write(@by, 0, osize);
                    //pbUpdate.Value = totalDownloadedByte;
                    osize = stream.Read(@by, 0, @by.Length);
                }
                sw.Close();
                stream.Close();

                File.SetLastWriteTime(FileName, fileDate);
            }
            catch //(Exception ex)
            {
                if (fileResponse != null)
                {
                    fileResponse.Close();
                }

                if (webResponse != null)
                {
                    webResponse.Close();
                }
            }
        }
Beispiel #12
0
        void Button1Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "HTML|*.html";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.Clear();
                //INIZIALIZZAZIONE VARIABILI PER FILTRI CON CHECKBOX
                string puntoSpazio  = "";
                string spazioPunto  = "";
                string puntoSingolo = "";
                if (checkBox1.Checked)
                {
                    puntoSpazio = ". ";
                }
                if (checkBox2.Checked)
                {
                    spazioPunto = " .";
                }
                if (checkBox3.Checked)
                {
                    puntoSingolo = ".";
                }
                //--------------------------------------------------

                string filePath = ofd.FileName;                                 //OTTIENE LA STRINGA CORRISPONDENTE AL PERCORSO DEL FILE SELEZIONATO
                //----------CODICE-PER-PRENDERE-IL-SORGENTE-DELL'HTML-------------
                FileWebRequest  request    = (FileWebRequest)WebRequest.Create(filePath);
                FileWebResponse response   = (FileWebResponse)request.GetResponse();
                StreamReader    sr         = new StreamReader(response.GetResponseStream());
                String          htmlSource = sr.ReadToEnd();
                //----------------------------------------------------------------
                //Dentro htmlSource c'è una il sorgente

                //string[] testSplit = htmlSource.Split("<tr id=\"r1\">");
                string[] htmlSplitted = null;
                htmlSplitted = htmlSource.Split(new[] { "<tr id=" }, StringSplitOptions.None);



                //richTextBox1.Text = htmlSplitted2[0];

                double t = 100 / htmlSplitted.Length;

                for (int i = 1; i < htmlSplitted.Length; i++)
                {
                    string[] htmlSplitted1 = null;
                    htmlSplitted1 = htmlSplitted[i].Split(new[] { "<td class=\"td2\">" }, StringSplitOptions.None);
                    //--------------------------------MODIFICHE PER AGGIUNGERE ID E SOURCE-------------------------------------------------
                    string[] htmlSplitSup = null;                                                                                       //Variabile che creo per prendere anche id source
                    htmlSplitSup = htmlSplitted1[0].Split(new[] { "</td>" }, StringSplitOptions.None);

                    string[] htmlSplitSup2 = null;
                    htmlSplitSup2 = htmlSplitSup[0].Split(new[] { ">" }, StringSplitOptions.None);

                    string[] htmlSplitSup3 = null;
                    htmlSplitSup3 = htmlSplitSup[1].Split(new[] { "<td>" }, StringSplitOptions.None);

                    string id     = htmlSplitSup2[2];
                    string source = htmlSplitSup3[1];
                    //-----------------------------------------------------------------------------------------------------------------
                    if (htmlSplitted1.Length < 2)
                    {
                        continue;
                    }
                    //LA STRINGA DI TARGHE E' DENTRO htmlSplitted2
                    string[] htmlSplitted2 = null;
                    htmlSplitted2 = htmlSplitted1[1].Split(new[] { "</td>" }, StringSplitOptions.None);


                    if (htmlSplitted2[0].Contains(puntoSpazio) || htmlSplitted2[0].Contains(spazioPunto) || htmlSplitted2[0].Contains(puntoSingolo))
                    {
                        richTextBox1.Text += id + " | " + source + " | " + htmlSplitted2[0] + "\n";
                    }
                }
                //richTextBox1.Text = ris;
                sr.Close();
            }
        }
Beispiel #13
0
        /// <summary>
        /// Process the request (once). Get the response (page content, cookies, ...).
        /// </summary>
        private void ProcessRequest()
        {
            if (RequestProcessed)
            {
                return; // test, unlocked
            }
            lock (this)
            {
                if (RequestProcessed)
                {
                    return;                   // double checked lock, test again
                }
                RequestProcessed = true;

                StorageValue data = (StorageValue)DataCache.GetItem(StorageKey,
                                                                    delegate(out DateTime expiration)
                {
                    WebRequest webreq = WebRequest.Create(ContextUri);

                    string RespContent           = null;
                    CookieCollection RespCookies = null;

                    if (webreq is HttpWebRequest)
                    {
                        HttpWebRequest req = (HttpWebRequest)webreq;

                        req.Referer           = (RefererContext != null && RefererContext.ContextUri != null) ? RefererContext.ContextUri.AbsoluteUri : null;
                        req.UserAgent         = DefaultUserAgent;
                        req.Timeout           = 30000;
                        req.AllowAutoRedirect = false;
                        req.KeepAlive         = false;
                        var cookieJar         = req.CookieContainer = new CookieContainer();
                        if (RefererContext != null && RefererContext.Cookies != null)
                        {       // TODO: filter cookies by domain and path
                            req.CookieContainer.Add(RefererContext.Cookies);
                        }
                        //req.Headers.Add("Accept-Language", "en,cs");

                        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

                        // page content
                        Stream RespStream = resp.GetResponseStream();
                        RespContent       = new StreamReader(RespStream /*, Encoding.GetEncoding(resp.ContentEncoding)*/).ReadToEnd();
                        RespStream.Close();

                        // cookies
                        foreach (Cookie c in cookieJar.GetCookies(req.RequestUri))
                        {
                            if (RespCookies == null)
                            {
                                RespCookies = new CookieCollection();
                            }

                            RespCookies.Add(c);
                        }

                        // TODO: headers (language, cache expire, content type, encoding, Response URI, ...)

                        // close the response
                        resp.Close();
                    }
                    else if (webreq is FileWebRequest)
                    {
                        FileWebRequest req   = (FileWebRequest)webreq;
                        FileWebResponse resp = (FileWebResponse)req.GetResponse();

                        // page content
                        Stream RespStream = resp.GetResponseStream();
                        RespContent       = new StreamReader(RespStream /*, Encoding.GetEncoding(resp.ContentEncoding)*/).ReadToEnd();
                        RespStream.Close();

                        // close the response
                        resp.Close();
                    }

                    expiration = DateTime.Now.AddHours(1.0);        // TODO: time based on HTML header or HtmlContext parameters

                    return(new StorageValue()
                    {
                        Content = RespContent, Cookies = RespCookies
                    });
                }
                                                                    );

                _Content = data.Content;
                _Cookies = data.Cookies;
            }
        }
Beispiel #14
0
        /// <summary>
        /// Can be called syncronized to get a Http Web ResponseStream.
        /// </summary>
        /// <param name="method">The HTTP request method</param>
        /// <param name="address">Url to request</param>
        /// <param name="newAddress">out string. return a new url, if the original requested is permanent moved</param>
        /// <param name="credentials">Url credentials</param>
        /// <param name="userAgent"></param>
        /// <param name="proxy">Proxy to use</param>
        /// <param name="ifModifiedSince">Header date</param>
        /// <param name="eTag">Header tag</param>
        /// <param name="timeout">Request timeout. E.g. 60 * 1000, means one minute timeout.
        /// If zero or less than zero, the default timeout of one minute will be used</param>
        /// <param name="responseResult">out. Result of the request</param>
        /// <param name="cookie">The cookie associated with the request</param>
        /// <param name="body">The body of the HTTP request (if it is a POST)</param>
        /// <param name="additonalHeaders">These are additional headers that are being specified to the Web request</param>
        /// <returns>Stream</returns>
        public static Stream GetResponseStream(HttpMethod method, string address, out string newAddress,
                                               ICredentials credentials,
                                               string userAgent,
                                               IWebProxy proxy, ref DateTime ifModifiedSince, ref string eTag,
                                               int timeout, out RequestResult responseResult, Cookie cookie, string body,
                                               WebHeaderCollection additonalHeaders)
        {
            bool      useDefaultCred    = false;
            int       requestRetryCount = 0;
            const int MaxRetries        = 25;

            newAddress = null;

send_request:

            string requestUri = address;

            if (useDefaultCred)
            {
                credentials = CredentialCache.DefaultCredentials;
            }

            WebResponse wr =
                GetResponse(method, address, credentials, userAgent, proxy, ifModifiedSince, eTag, timeout, cookie, body,
                            additonalHeaders);

            HttpWebResponse response     = wr as HttpWebResponse;
            FileWebResponse fileresponse = wr as FileWebResponse;

            if (response != null)
            {
                if (HttpStatusCode.OK == response.StatusCode ||
                    HttpExtendedStatusCode.IMUsed == (HttpExtendedStatusCode)response.StatusCode)
                {
                    responseResult = RequestResult.OK;
                    // stream will be disposed on response.Close():
                    Stream ret = MakeSeekableStream(response.GetResponseStream()); //GetDeflatedResponse(response);
                    response.Close();
                    return(ret);
                }
                if ((response.StatusCode == HttpStatusCode.MovedPermanently) ||
                    (response.StatusCode == HttpStatusCode.Moved))
                {
                    newAddress = HtmlHelper.ConvertToAbsoluteUrl(response.Headers["Location"], address, false);
                    address    = newAddress;
                    response.Close();

                    if (requestRetryCount < MaxRetries)
                    {
                        requestRetryCount++;
                        goto send_request;
                    }
                }
                else if (IsUnauthorized(response.StatusCode))
                {
                    //try with default credentials

                    useDefaultCred = true;
                    response.Close();

                    if (requestRetryCount < MaxRetries)
                    {
                        requestRetryCount++;
                        goto send_request;
                    }
                }
                else if (IsRedirect(response.StatusCode))
                {
                    address = HtmlHelper.ConvertToAbsoluteUrl(response.Headers["Location"], address, false);
                    response.Close();

                    if (requestRetryCount < MaxRetries)
                    {
                        requestRetryCount++;
                        goto send_request;
                    }
                }
                else if (IsAccessForbidden(response.StatusCode) &&
                         requestUri.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ClientCertificateRequiredException();
                }
                else if (response.StatusCode == HttpStatusCode.Gone)
                {
                    throw new ResourceGoneException();
                }
                else
                {
                    string statusCode = response.StatusDescription;
                    if (String.IsNullOrEmpty(statusCode))
                    {
                        statusCode = response.StatusCode.ToString();
                    }
                    response.Close();
                    throw new WebException(String.Format("Request of '{0}' gets unexpected HTTP response: {1}",
                                                         requestUri, statusCode));
                }

                // unauthorized more than MaxRetries
                if (IsUnauthorized(response.StatusCode))
                {
                    response.Close();
                    throw new ResourceAuthorizationException();
                }

                //we got a moved, redirect more than MaxRetries
                string returnCode = response.StatusDescription;
                if (String.IsNullOrEmpty(returnCode))
                {
                    returnCode = response.StatusCode.ToString();
                }
                response.Close();
                throw new WebException(String.Format("Request of '{0}' gets repeated HTTP response: {1}", requestUri,
                                                     returnCode));
            }

            if (fileresponse != null)
            {
                responseResult = RequestResult.OK;
                // stream will be disposed on response.Close():
                Stream ret = MakeSeekableStream(fileresponse.GetResponseStream()); //GetDeflatedResponse(fileresponse);
                fileresponse.Close();
                return(ret);
            }
            throw new ApplicationException("no handler for WebResponse. Address: " + requestUri);
        }
Beispiel #15
0
        /// <summary>
        /// Called to extract a list from the specified URL.
        /// </summary>
        /// <param name=”url”>The URL to extract the list
        /// from.</param>
        /// <param name=”listType”>What type of list, specify
        /// its beginning tag (i.e. <UL>)</param>
        /// <param name=”optionList”>Which list to search,
        /// zero for first.</param>

        public void ProcessTable(Uri url, int tableNum)
        {

            //ignore bad cert code
            IgnoreBadCertificates();

            //code to allow program with work with different authentication schemes
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            List<String> list = new List<String>();
            //WebRequest http = HttpWebRequest.Create(url);
            FileWebRequest http = (FileWebRequest)WebRequest.Create(url);
            //HttpWebResponse response = (HttpWebResponse)http.GetResponse();
            FileWebResponse response = (FileWebResponse)http.GetResponse();
            Stream istream = response.GetResponseStream();
            ParseHTML parse = new ParseHTML(istream);
            StringBuilder buffer = new StringBuilder();
            bool capture = false;
            Advance(parse, "table", tableNum);
            int ch;
            while ((ch = parse.Read()) != -1)
            {
                if (ch == 0)
                {
                    HTMLTag tag = parse.Tag;
                    if (String.Compare(tag.Name, "tr", true) == 0)
                    {
                        list.Clear();
                        capture = false;
                        buffer.Length = 0;
                    }
                    else if (String.Compare(tag.Name, "/tr", true) == 0)
                    {
                        if (list.Count > 0)
                        {
                            ProcessTableRow(list);
                            list.Clear();
                        }
                    }
                    else if (String.Compare(tag.Name, "td", true) == 0)
                    {
                        if (buffer.Length > 0)
                            list.Add(buffer.ToString());
                        buffer.Length = 0;
                        capture = true;
                    }
                    else if (String.Compare(tag.Name, "/td", true) == 0)
                    {
                        list.Add(buffer.ToString());
                        buffer.Length = 0;
                        capture = false;
                    }
                    else if (String.Compare(tag.Name, "/table", true) == 0)
                    {
                        break;
                    }
                }
                else
                {
                    if (capture)
                        buffer.Append((char)ch);
                }
            }
        }//end ProcessTable
Beispiel #16
0
        public override TaskResultType Execute(DownloadRequestSession objDownloadRequestSession)
        {
            TaskResultType enuTaskResult = TaskResultType.Completed;

            objDownloadRequestSession.UpdateProgess();

            FileWebRequest objWebRequest = (FileWebRequest)WebRequest.Create(base.Source);

            objWebRequest.Proxy       = ProxySettings.DefaultProxy;
            objWebRequest.Credentials = ((base.Credentials != null) ? base.Credentials.CreateNetworkCredentials() : null);
            objWebRequest.Timeout     = base.TimeOut;

            using (FileWebResponse objWebResponse = (FileWebResponse)objWebRequest.GetResponse())
            {
                objDownloadRequestSession.DownloadStats.Size = objWebResponse.ContentLength;
                objDownloadRequestSession.UpdateProgess();

                bool blnFileExists = FileManager.Exists(base.Destination);
                if (blnFileExists == true)
                {
                    FileManager.Delete(base.Destination, true);
                }

                string strDirectory       = Path.GetDirectoryName(base.Destination);
                bool   blnDirectoryExists = DirectoryManager.Exists(strDirectory);
                if (blnDirectoryExists == false)
                {
                    DirectoryManager.Create(strDirectory);
                }

                using (Stream objResponseStream = objWebResponse.GetResponseStream())
                {
                    using (FileStream objFileStream = new FileStream(base.Destination, FileMode.Create, FileAccess.Write))
                    {
                        byte[] bytBuffer    = new byte[DefaultBufferSize];
                        int    intBytesRead = objResponseStream.Read(bytBuffer, 0, bytBuffer.Length);

                        while ((intBytesRead != 0) && (objDownloadRequestSession.JobTicket.Cancelled == false))
                        {
                            objFileStream.Write(bytBuffer, 0, intBytesRead);
                            objFileStream.Flush();

                            objDownloadRequestSession.DownloadStats.BytesReceived += intBytesRead;
                            objDownloadRequestSession.UpdateProgess();

                            intBytesRead = objResponseStream.Read(bytBuffer, 0, bytBuffer.Length);
                        }
                    }
                }

                objDownloadRequestSession.DownloadStats.EndTime = DateTime.Now;
                objDownloadRequestSession.UpdateProgess();

                if (objDownloadRequestSession.JobTicket.Cancelled == true)
                {
                    FileManager.Delete(base.Destination);
                }
            }

            enuTaskResult = ((objDownloadRequestSession.JobTicket.Cancelled == true) ? TaskResultType.Cancelled : TaskResultType.Completed);
            return(enuTaskResult);
        }
Beispiel #17
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="strUrl"></param>
        private void DownloadFile(string FileName, string strUrl)
        {
            HttpWebRequest  webRequest;
            HttpWebResponse webResponse = null;
            FileWebRequest  fileRequest;
            FileWebResponse fileResponse = null;
            bool            isFile       = false;

            try
            {
                System.Globalization.DateTimeFormatInfo dfi = null;
                System.Globalization.CultureInfo        ci  = null;
                ci  = new System.Globalization.CultureInfo("zh-CN");
                dfi = new System.Globalization.DateTimeFormatInfo();

                //WebRequest wr = WebRequest.Create("");

                //System.Net.WebResponse w=wr.
                DateTime      fileDate;
                long          totalBytes;
                DirectoryInfo theFolder = new DirectoryInfo(strTemp);
                string        fileName  = theFolder + FileName;
                if (strUrl.EndsWith(".exe"))
                {
                    strUrl = strUrl.Replace(".exe", ".dll");
                }
                isFile = (HttpWebRequest.Create(strUrl) is FileWebRequest);

                if (isFile)
                {
                    fileRequest  = (FileWebRequest)FileWebRequest.Create(strUrl);
                    fileResponse = (FileWebResponse)fileRequest.GetResponse();
                    if (fileResponse == null)
                    {
                        return;
                    }
                    fileDate   = DateTime.Now;
                    totalBytes = fileResponse.ContentLength;
                }
                else
                {
                    webRequest  = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                    webResponse = (HttpWebResponse)webRequest.GetResponse();
                    if (webResponse == null)
                    {
                        return;
                    }
                    fileDate   = webResponse.LastModified;
                    totalBytes = webResponse.ContentLength;
                }

                pbUpdate.Maximum = Convert.ToInt32(totalBytes);

                Stream stream;
                if (isFile)
                {
                    stream = fileResponse.GetResponseStream();
                }
                else
                {
                    stream = webResponse.GetResponseStream();
                }
                FileStream sw = new FileStream(fileName, FileMode.Create);
                int        totalDownloadedByte = 0;
                Byte[]     @by   = new byte[1024];
                int        osize = stream.Read(@by, 0, @by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    Application.DoEvents();
                    sw.Write(@by, 0, osize);
                    pbUpdate.Value = totalDownloadedByte;
                    osize          = stream.Read(@by, 0, @by.Length);
                }
                sw.Close();
                stream.Close();

                //System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName(AppName);

                ////关闭原有应用程序的所有进程
                //foreach (System.Diagnostics.Process pro in proc)
                //{
                //    pro.Kill();
                //}


                //DirectoryInfo theFolder = new DirectoryInfo(Path.GetTempPath() + "ysy");
                if (theFolder.Exists)
                {
                    foreach (FileInfo theFile in theFolder.GetFiles())
                    {
                        if (theFile.Name != UpdExeName + ".exe" && theFile.Name != UpdExeName + ".pdb" && theFile.Name != "Config.xml")
                        {
                            //如果临时文件夹下存在与应用程序所在目录下的文件同名的文件,则删除应用程序目录下的文件
                            if (File.Exists(this.m_workPath + "\\" + Path.GetFileName(theFile.FullName)))
                            {
                                File.Delete(this.m_workPath + "\\" + Path.GetFileName(theFile.FullName));
                                //将临时文件夹的文件移到应用程序所在的目录下
                                File.Move(theFile.FullName, this.m_workPath + "\\" + Path.GetFileName(theFile.FullName));
                            }
                        }
                    }
                }
                File.SetLastWriteTime(FileName, fileDate);
            }
            catch (Exception ex)
            {
                if (fileResponse != null)
                {
                    fileResponse.Close();
                }

                if (webResponse != null)
                {
                    webResponse.Close();
                }

                // MessageBox.Show(ex.Message);
                lblStatus.Text = "更新出错!" + ex.Message;
                lblStatus.Refresh();
            }
        }
Beispiel #18
0
        /// <summary>
        /// WebResponse processing.
        /// </summary>
        private void ProcessResponse(RequestState state)
        {
            try
            {
                HttpWebResponse httpResponse = state.Response as HttpWebResponse;
                FileWebResponse fileResponse = state.Response as FileWebResponse;
                NntpWebResponse nntpResponse = state.Response as NntpWebResponse;

                if (httpResponse != null)
                {
                    if (httpResponse.ResponseUri != state.RequestUri)
                    {
                        Log.Debug(
                            String.Format("httpResponse.ResponseUri != state.RequestUri: \r\n'{0}'\r\n'{1}'",
                                          httpResponse.ResponseUri, state.RequestUri));
                    }

                    if (HttpStatusCode.OK == httpResponse.StatusCode ||
                        HttpExtendedStatusCode.IMUsed == (HttpExtendedStatusCode)httpResponse.StatusCode)
                    {
                        HttpCookieManager.GetCookies(httpResponse);

                        // provide last request Uri and ETag:
                        state.RequestParams.ETag = httpResponse.Headers.Get("ETag");
                        try
                        {
                            state.RequestParams.LastModified = httpResponse.LastModified;
                        }
                        catch (Exception lmEx)
                        {
                            Log.Debug("httpResponse.LastModified() parse failure: " + lmEx.Message);
                            // Build in header parser failed on provided date format
                            // Try our own parser (last chance)
                            try
                            {
                                state.RequestParams.LastModified =
                                    DateTimeExt.ParseRfc2822DateTime(httpResponse.Headers.Get("Last-Modified"));
                            }
                            catch (FormatException)
                            {
                                /* ignore */
                            }
                        }

                        state.ResponseStream = httpResponse.GetResponseStream();
                        state.ResponseStream.BeginRead(state.ReadBuffer, 0, RequestState.BUFFER_SIZE,
                                                       ReadCallback, state);
                        // async read started, so we are done here:
                        Log.Debug("ProcessResponse() web response OK: " + state.RequestUri);

                        return;
                    }

                    if (httpResponse.StatusCode == HttpStatusCode.NotModified)
                    {
                        HttpCookieManager.GetCookies(httpResponse);

                        string eTag = httpResponse.Headers.Get("ETag");
                        // also if it was not modified, we receive a httpResponse.LastModified with current date!
                        // so we did not store it (is is just the same as last-retrived)
                        // provide last request Uri and ETag:
                        state.OnRequestCompleted(state.InitialRequestUri, state.RequestParams.RequestUri, eTag, MinValue,
                                                 RequestResult.NotModified);
                        // cleanup:
                        FinalizeWebRequest(state);
                    }
                    else if ((httpResponse.StatusCode == HttpStatusCode.MovedPermanently) ||
                             (httpResponse.StatusCode == HttpStatusCode.Moved))
                    {
                        state.RetryCount++;
                        if (state.RetryCount > RequestState.MAX_RETRIES)
                        {
                            // there is no WebExceptionStatus.UnknownError in .NET 1.0 !!!
                            throw new WebException("Repeated HTTP httpResponse: " + httpResponse.StatusCode,
                                                   null, WebExceptionStatus.RequestCanceled, httpResponse);
                        }

                        string url2 = httpResponse.Headers["Location"];
                        //Check for any cookies
                        HttpCookieManager.GetCookies(httpResponse);

                        state.MovedPermanently = true;
                        //Remove Url from queue
                        _queuedRequests.Remove(state.InitialRequestUri.CanonicalizedUri());

                        Log.Debug("ProcessResponse() Moved: '" + state.InitialRequestUri + " to " + url2);

                        // Enqueue the request with the new Url.
                        // We raise the queue priority a bit to get the retry request closer to the just
                        // finished one. So the user get better feedback, because the whole processing
                        // of one request (including the redirection/moved/... ) is visualized as one update
                        // action.


                        Uri req;
                        //Try absolute first
                        if (!Uri.TryCreate(url2, UriKind.Absolute, out req))
                        {
                            // Try relative
                            if (!Uri.TryCreate(httpResponse.ResponseUri, url2, out req))
                            {
                                throw new WebException(
                                          string.Format(
                                              "Original resource temporary redirected. Request new resource at '{0}{1}' failed: ",
                                              httpResponse.ResponseUri, url2));
                            }
                        }

                        RequestParameter rqp = RequestParameter.Create(req, state.RequestParams);
                        QueueRequestAgain(rqp, state.Priority + 1, state);
                    }
                    else if (IsRedirect(httpResponse.StatusCode))
                    {
                        state.RetryCount++;
                        if (state.RetryCount > RequestState.MAX_RETRIES)
                        {
                            // there is no WebExceptionStatus.UnknownError in .NET 1.0 !!!
                            throw new WebException("Repeated HTTP httpResponse: " + httpResponse.StatusCode,
                                                   null, WebExceptionStatus.RequestCanceled, httpResponse);
                        }

                        string url2 = httpResponse.Headers["Location"];
                        //Check for any cookies
                        HttpCookieManager.GetCookies(httpResponse);

                        //Remove Url from queue
                        _queuedRequests.Remove(state.InitialRequestUri.CanonicalizedUri());

                        Log.Debug("ProcessResponse() Redirect: '" + state.InitialRequestUri + " to " + url2);
                        // Enqueue the request with the new Url.
                        // We raise the queue priority a bit to get the retry request closer to the just
                        // finished one. So the user get better feedback, because the whole processing
                        // of one request (including the redirection/moved/... ) is visualized as one update
                        // action.

                        Uri req;
                        //Try absolute first
                        if (!Uri.TryCreate(url2, UriKind.Absolute, out req))
                        {
                            // Try relative
                            if (!Uri.TryCreate(httpResponse.ResponseUri, url2, out req))
                            {
                                throw new WebException(
                                          string.Format(
                                              "Original resource temporary redirected. Request new resource at '{0}{1}' failed: ",
                                              httpResponse.ResponseUri, url2));
                            }
                        }


                        RequestParameter rqp =
                            RequestParameter.Create(req, RebuildCredentials(state.RequestParams.Credentials, url2),
                                                    state.RequestParams);
                        QueueRequestAgain(rqp, state.Priority + 1, state);
                    }
                    else if (IsUnauthorized(httpResponse.StatusCode))
                    {
                        if (state.RequestParams.Credentials == null)
                        {
                            // no initial credentials, try with default credentials
                            state.RetryCount++;

                            //Remove Url from queue
                            _queuedRequests.Remove(state.InitialRequestUri.CanonicalizedUri());

                            // Enqueue the request with the new Url.
                            // We raise the queue priority a bit to get the retry request closer to the just
                            // finished one. So the user get better feedback, because the whole processing
                            // of one request (including the redirection/moved/... ) is visualized as one update
                            // action.
                            RequestParameter rqp =
                                RequestParameter.Create(CredentialCache.DefaultCredentials, state.RequestParams);
                            QueueRequestAgain(rqp, state.Priority + 1, state);
                        }
                        else
                        {
                            // failed with provided credentials

                            if (state.RequestParams.SetCookies)
                            {
                                // one more request without cookies

                                state.RetryCount++;

                                //Remove Url from queue
                                _queuedRequests.Remove(state.InitialRequestUri.CanonicalizedUri());

                                // Enqueue the request with the new Url.
                                // We raise the queue priority a bit to get the retry request closer to the just
                                // finished one. So the user get better feedback, because the whole processing
                                // of one request (including the redirection/moved/... ) is visualized as one update
                                // action.
                                RequestParameter rqp = RequestParameter.Create(false, state.RequestParams);
                                QueueRequestAgain(rqp, state.Priority + 1, state);
                            }
                            else
                            {
                                throw new ResourceAuthorizationException();
                            }
                        }
                    }
                    else if (IsAccessForbidden(httpResponse.StatusCode) &&
                             state.InitialRequestUri.Scheme == "https")
                    {
                        throw new ClientCertificateRequiredException();
                    }
                    else if (httpResponse.StatusCode == HttpStatusCode.Gone)
                    {
                        throw new ResourceGoneException();
                    }
                    else
                    {
                        string statusDescription = httpResponse.StatusDescription;
                        if (String.IsNullOrEmpty(statusDescription))
                        {
                            statusDescription = httpResponse.StatusCode.ToString();
                        }

                        string htmlStatusMessage = null;
                        try
                        {
                            htmlStatusMessage = new StreamReader(httpResponse.GetResponseStream()).ReadToEnd();
                        }
                        catch { }

                        if (String.IsNullOrEmpty(htmlStatusMessage))
                        {
                            throw new WebException("Unexpected HTTP Response: " + statusDescription);
                        }

                        if (htmlStatusMessage.Contains("<"))
                        {
                            throw new WebException(htmlStatusMessage);
                        }

                        throw new WebException(
                                  "<html><head><title>Unexpected HTTP Response</title></head><body><h2>Unexpected HTTP Response: " +
                                  statusDescription + "</h2><p>" + htmlStatusMessage + "</p></html>");
                    }
                }
                else if (fileResponse != null)
                {
                    string reqFile = fileResponse.ResponseUri.LocalPath;

                    if (File.Exists(reqFile))
                    {
                        DateTime lwt = File.GetLastWriteTime(reqFile);
                        state.RequestParams.ETag         = lwt.ToString();
                        state.RequestParams.LastModified = lwt;
                    }

                    state.ResponseStream = fileResponse.GetResponseStream();
                    state.ResponseStream.BeginRead(state.ReadBuffer, 0, RequestState.BUFFER_SIZE,
                                                   ReadCallback, state);
                    // async read started, so we are done here:
                    Log.Debug("ProcessResponse() file response OK: " + state.RequestUri);

                    return;
                }
                else if (nntpResponse != null)
                {
                    state.RequestParams.LastModified = DateTime.Now;
                    state.ResponseStream             = nntpResponse.GetResponseStream();
                    state.ResponseStream.BeginRead(state.ReadBuffer, 0, RequestState.BUFFER_SIZE,
                                                   ReadCallback, state);
                    // async read started, so we are done here:
                    Log.Debug("ProcessResponse() nntp response OK: " + state.RequestUri);

                    return;
                }
                else
                {
                    Debug.Assert(false,
                                 "ProcessResponse(): unhandled WebResponse type: " +
                                 state.Response.GetType());
                    FinalizeWebRequest(state);
                }
            }
            catch (ThreadAbortException)
            {
                FinalizeWebRequest(state);
                // ignore, just return
            }
            catch (Exception ex)
            {
                Log.Debug("ProcessResponse() exception: " + state.RequestUri + " :" + ex.Message);
                state.OnRequestException(state.InitialRequestUri, ex);
                FinalizeWebRequest(state);
            }
        }