Содержание блога

суббота, 19 июня 2010 г.

Послать блок текста на сервер методом POST

Первая попытка, почемуто в ответ тишина


#region попытка раз
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);  
            textBoxLog.Text += "Формирование POST запроса...request" + "\r\n";
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded" + "\r\n";
            textBoxLog.Text += "request.RequestUri = " + request.RequestUri.AbsoluteUri + "\r\n";
            sqlpkt = sql;
            textBoxLog.Text += "Формирование POST запроса...BeginGetRequestStream....";
            request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
            textBoxLog.Text += "ok \r\n";

            #endregion попытка раз
....
        #region RequestReady ResponseReady

        void RequestReady(IAsyncResult asyncResult)
        {
            textBoxLog.Text += "RequestReady\r\n";
            HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
            Stream stream = request.EndGetRequestStream(asyncResult);

            // Hack for solving multi-threading problem  
            // I think this is a bug  
            this.Dispatcher.BeginInvoke(delegate()
            {
                // Send the post variables  
                textBoxLog.Text += "RequestReady: Send the post variables.... ";
                StreamWriter writer = new StreamWriter(stream);
                writer.WriteLine("sessionid="+sessionID);
                writer.WriteLine("sql="+sqlpkt);
                writer.Flush();
                writer.Close();
                textBoxLog.Text += "ok\r\n";
                textBoxLog.Text += "RequestReady: request.BeginGetResponse\r\n";
                request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
                textBoxLog.Text += "RequestReady: request.BeginGetResponse....ok\r\n";
            });
        }  

        // Get the Result  
        void ResponseReady(IAsyncResult asyncResult)
        {
            textBoxLog.Text += "ResponseReady\r\n";
            HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);

            this.Dispatcher.BeginInvoke(delegate()
            {
                textBoxLog.Text += "ResponseReady: this.Dispatcher.BeginInvoke(delegate() \r\n";
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                // get the result text  
                string result = reader.ReadToEnd();
                textBoxLog.Text += "reader result: " + result + "\r\n";
            });
        }
        #endregion RequestReady ResponseReady
 

Комментариев нет:

Отправить комментарий