Exemple #1
0
        /// <summary>
        /// Call this method to send the request/receive the response
        /// </summary>
        public void Execute()
        {
            Task.Factory.StartNew(() => {
                try {
                    if (JsonRequest != null && !JsonRequest.EndsWith("}"))
                    {
                        JsonRequest.Append("}");
                    }

                    // init request
                    _httpRequest = WebRequest.Create(Url) as HttpWebRequest;
                    if (_httpRequest != null)
                    {
                        _httpRequest.Proxy            = Config.Instance.GetWebClientProxy();
                        _httpRequest.Method           = Method.ToString().ToUpper();
                        _httpRequest.ContentType      = "application/json";
                        _httpRequest.UserAgent        = Config.GetUserAgent;
                        _httpRequest.ReadWriteTimeout = TimeOut;
                        _httpRequest.Timeout          = TimeOut;
                    }
                    if (OnInitHttpWebRequest != null)
                    {
                        OnInitHttpWebRequest(_httpRequest);
                    }

                    if (Method == WebRequestMethod.Post)
                    {
                        if (JsonRequest == null)
                        {
                            ResponseException = new Exception("No request set!");
                        }
                        else
                        {
                            // start posting
                            if (PostRequest())
                            {
                                // get the response
                                GetResponse();
                            }
                        }
                    }
                    else if (Method == WebRequestMethod.Get)
                    {
                        // get the response
                        GetResponse();
                    }

                    if (OnRequestEnded != null)
                    {
                        OnRequestEnded(this);
                    }
                } catch (WebException e) {
                    ResponseException = e;
                    HandleWebException(e);
                } catch (Exception e) {
                    ResponseException  = e;
                    StatusCodeResponse = HttpStatusCode.BadRequest;
                }
            });
        }
Exemple #2
0
        /// <summary>
        /// Append name: "value" to the request
        /// </summary>
        public void AddToReq(string name, string value)
        {
            if (JsonRequest == null)
            {
                JsonRequest = new StringBuilder("{");
            }
            else
            {
                JsonRequest.Append(",");
            }

            if (_jsSerializer == null)
            {
                _jsSerializer = new JavaScriptSerializer();
            }

            JsonRequest.Append("\"");
            JsonRequest.Append(name);
            JsonRequest.Append("\": ");
            JsonRequest.Append(_jsSerializer.Serialize(value));
        }