Example #1
0
 private void PostQuery(string URL, Dictionary<string, string> postData, CallBack onSuccess, CallBack onError)
 {
     //string boundary = "----------" + DateTime.Now.Ticks.ToString();
     HttpWebRequest request = WebRequest.CreateHttp(new Uri(URL)); //создаем запрос
     request.ContentType = "application/x-www-form-urlencoded";
     //request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
     //request.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*";
     request.UserAgent =
         "Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729)";
     request.Method = "POST";
     request.Headers["Accept-Charset"] = "windows-1251,utf-8;q=0.7,*;q=0.3";
     request.Headers["Accept-Language"] = "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4";
     request.CookieContainer = this.cookie;
     Thread thead = new Thread(delegate()
     {
         request.BeginGetRequestStream(new AsyncCallback(asynchronousResultRequest =>
         {
             HttpWebRequest requestStateRequest = (HttpWebRequest)asynchronousResultRequest.AsyncState;
             Stream postStream = requestStateRequest.EndGetRequestStream(asynchronousResultRequest);
             byte[] byteArray = Encoding.UTF8.GetBytes(postData.ToUrlData());
             postStream.Write(byteArray, 0, byteArray.Length);
             postStream.Close();
             requestStateRequest.BeginGetResponse(new AsyncCallback(asynchronousResultResponse =>
             {
                 HttpWebRequest requestStateResponse = (HttpWebRequest)asynchronousResultResponse.AsyncState;
                 try
                 {
                     HttpWebResponse response = (HttpWebResponse)requestStateResponse.EndGetResponse(asynchronousResultResponse);
                     var Location = "";
                     if (response.Headers["Location"] != null)
                     {
                         Location = response.Headers["Location"];
                     }
                     Stream streamResponse = response.GetResponseStream();
                     StreamReader streamRead = new StreamReader(streamResponse);
                     string responseString = streamRead.ReadToEnd();
                     streamResponse.Close();
                     streamRead.Close();
                     response.Close();
                     onSuccess(responseString);
                 }
                 catch (WebException e)
                 {
                     onError(e);
                 }
             }), requestStateRequest);
         }), request);
     });
     thead.Start();
 }
Example #2
0
 private void GetQuery(string URL, Dictionary<string, string> getData, CallBack onSuccess, CallBack onError)
 {
     string resultURL = URL;
     resultURL += "?";
     resultURL += getData.ToUrlData();
     HttpWebRequest request = WebRequest.CreateHttp(new Uri(resultURL)); //создаем запрос
     request.UserAgent =
         "Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729)";
     request.CookieContainer = this.cookie;
     Thread thead = new Thread(delegate()
     {
         request.BeginGetResponse(new AsyncCallback(asynchronousResult =>
         {
             HttpWebRequest requestState = (HttpWebRequest)asynchronousResult.AsyncState;
             try
             {
                 HttpWebResponse response = (HttpWebResponse)requestState.EndGetResponse(asynchronousResult);
                 Stream streamResponse = response.GetResponseStream();
                 StreamReader streamRead = new StreamReader(streamResponse);
                 string responseString = streamRead.ReadToEnd();
                 streamResponse.Close();
                 streamRead.Close();
                 response.Close();
                 onSuccess(responseString);
             }
             catch (WebException e)
             {
                 onError(e);
             }
         }), request);
     });
     thead.Start();
 }