Example #1
0
        private void SaveXML(string uri, string path, Cookie session)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            request.Method = "GET";
            // request.Accept = "text/html,application/xhtml+xml,application/xml";
            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(session);
            // request.CookieContainer.Add(new Cookie("_osm_location", "-0.12400650233031|51.526474001441|15|M", null, "www.openstreetmap.org"));
            request.Timeout = 1000 * 60 * 5;

            // HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            int counter = 0;
            HttpWebResponseWorker worker = new HttpWebResponseWorker(request);
            while (worker.IsBusy)
            {
                Log(string.Format("Please wait ... {0}", counter++));
                System.Threading.Thread.Sleep(500);
            }
            HttpWebResponse response = worker.Response;
            // if (response.ContentLength > 0)
            {
                const int bufferSize = 2048;
                System.IO.Stream strm = response.GetResponseStream();
                FileStream fs = File.Open(path, FileMode.Create);
                int length = 0;
                int bytesRead = 0;
                do
                {
                    byte[] buffer = new byte[bufferSize];
                    // int
                    bytesRead = strm.Read(buffer, 0, bufferSize);
                    fs.Write(buffer, 0, bytesRead);
                    length += bytesRead;
                    Log(string.Format("Saving {0} ...", length));
                    // } while (length < response.ContentLength);
                } while (bytesRead > 0);
                fs.Close();
            }
            response.Close();
        }
Example #2
0
        private void SavePost(string uri, string data, string path, Cookie session)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            // request.Accept = "text/html,application/xhtml+xml,application/xml";
            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(session);
            // request.CookieContainer.Add(new Cookie("_osm_location", "-0.12400650233031|51.526474001441|15|M", null, "www.openstreetmap.org"));
            byte[] byteArray = Encoding.UTF8.GetBytes(data);
            request.ContentLength = byteArray.Length;
            Stream streamRequest = request.GetRequestStream();
            streamRequest.Write(byteArray, 0, byteArray.Length);
            streamRequest.Close();

            // HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            int counter = 0;
            HttpWebResponseWorker worker = new HttpWebResponseWorker(request);
            while (worker.IsBusy)
            {
                Log(string.Format("Please wait ... {0}", counter++));
                System.Threading.Thread.Sleep(500);
            }
            HttpWebResponse response = worker.Response;
            if (response != null)
            {
                if (response.ContentLength > 0)
                {
                    const int bufferSize = 2048;
                    System.IO.Stream strm = response.GetResponseStream();
                    FileStream fs = File.Open(path, FileMode.Create);
                    int length = 0;
                    do
                    {
                        byte[] buffer = new byte[bufferSize];
                        int bytesRead = strm.Read(buffer, 0, bufferSize);
                        fs.Write(buffer, 0, bytesRead);
                        length += bytesRead;
                    } while (length < response.ContentLength);
                    fs.Close();
                }
                else
                    textBox1.Text = worker.Exception.Message;
                response.Close();
            }
        }