예제 #1
0
파일: Batch.cs 프로젝트: cjwang/SQLHelper
 /// <summary>
 /// Removes duplicate commands from the batch
 /// </summary>
 /// <returns>This</returns>
 public IBatch RemoveDuplicateCommands()
 {
     Commands = Commands.Distinct().ToList();
     Headers  = Headers.Distinct().ToList();
     return(this);
 }
예제 #2
0
        public void Request()
        {
            IsLoading  = true;
            Response   = "";
            StatusCode = "";
            System.Threading.Thread requestThread = new System.Threading.Thread(delegate()
            {
                try
                {
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(Url));

                    request.Method                = Method;
                    request.ContentType           = ContentType;
                    request.UserAgent             = UserAgent;
                    request.UseDefaultCredentials = UseDefaultCredentials;
                    request.PreAuthenticate       = PreAuthenticate;

                    if (!UseDefaultCredentials)
                    {
                        if (!UseBasicAuthorization)
                        {
                            request.Credentials = new NetworkCredential(Username, Password);
                            if (Domain != "" && Domain != null)
                            {
                                ((NetworkCredential)request.Credentials).Domain = Domain;
                            }
                        }
                        else
                        {
                            String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(Username + ":" + SecureStringToString(Password)));
                            request.Headers.Add("Authorization", "Basic " + encoded);
                        }
                    }

                    if (Headers.Distinct().Count() > 0)
                    {
                        Headers.Distinct().ToList().ForEach(a => request.Headers.Add(a.Key, a.Value));
                    }

                    if (Body != "" && Body != null && Method != "GET")
                    {
                        byte[] byteArray      = Encoding.UTF8.GetBytes(Body);
                        request.ContentLength = byteArray.Length;
                        Stream dataStream     = request.GetRequestStream();
                        dataStream.Write(byteArray, 0, byteArray.Length);
                        dataStream.Close();
                    }

                    try
                    {
                        using (var response = (HttpWebResponse)request.GetResponse())
                            using (var stream = response.GetResponseStream())
                                using (var reader = new StreamReader(stream))
                                {
                                    Response   = reader.ReadToEnd();
                                    StatusCode = ((int)response.StatusCode).ToString() + " (" + response.StatusCode.ToString() + ")";
                                    Dictionary <string, string> headers = new Dictionary <string, string>();
                                    for (var i = 0; i < response.Headers.Count; i++)
                                    {
                                        headers.Add(response.Headers.GetKey(i), response.Headers[i]);
                                    }
                                    ResponseHeaders = headers;
                                }
                    }
                    catch (WebException ex)
                    {
                        using (var response = (HttpWebResponse)ex.Response)
                            using (var stream = response.GetResponseStream())
                                using (var reader = new StreamReader(stream))
                                {
                                    Response   = reader.ReadToEnd();
                                    StatusCode = ((int)response.StatusCode).ToString() + " (" + response.StatusCode.ToString() + ")";
                                    Dictionary <string, string> headers = new Dictionary <string, string>();
                                    for (var i = 0; i < response.Headers.Count; i++)
                                    {
                                        headers.Add(response.Headers.GetKey(i), response.Headers[i]);
                                    }
                                    ResponseHeaders = headers;
                                }
                    }
                    catch (Exception ex)
                    {
                        Response = ex.Message;
                    }
                }
                catch (Exception ex)
                {
                    Response = "Sorry an unknown error occured in requesting. \r\n" + ex.Message;
                }
                finally
                {
                    IsLoading   = false;
                    var handler = RequestPosted;
                    if (handler != null)
                    {
                        handler(this, EventArgs.Empty);
                    }
                }
            });
            requestThread.Start();
        }