Ejemplo n.º 1
0
        private byte[] Post(string url, Dictionary <string, object> values)
        {
            byte[] response = null;
            using (WebClient client = new WebClientWithTimeout(this.Timeout, this.KeepAlive))
            {
                if (XHttpRequestOverride)
                {
                    client.Headers.Add("X-HTTP-Method-Override:" + Method.ToString());
                    client.Headers.Add("X-HTTP-Method:" + Method.ToString());
                    client.Headers.Add("X-METHOD-OVERRIDE:" + Method.ToString());
                }

                if (!string.IsNullOrWhiteSpace(Header))
                {
                    var headers = Header.Split('\n');
                    foreach (var header in headers)
                    {
                        if (!string.IsNullOrWhiteSpace(header))
                        {
                            client.Headers.Add(header);
                        }
                    }
                }
                NameValueCollection formData = null;
                switch (ContentType)
                {
                case HttpRequestContentType.JSON:
                    client.Headers.Add("Content-Type: application/json");
                    string json;
                    if (values.Count != 1 || IsPrimitive(values.First().Value))
                    {
                        json = Newtonsoft.Json.JsonConvert.SerializeObject(values);
                    }
                    else
                    {
                        json = Newtonsoft.Json.JsonConvert.SerializeObject(values.First());
                    }

                    response = client.UploadData(url, method: XHttpRequestOverride ? "POST" : Method.ToString(), data: Encoding.UTF8.GetBytes(json));
                    break;

                case HttpRequestContentType.FormData:

                    formData = new NameValueCollection();

                    foreach (var value in values)
                    {
                        formData.Add(value.Key, (value.Value ?? string.Empty).ToString());
                    }
                    //upload values já seta content-type para application/x-www-form-urlencoded
                    response = client.UploadValues(url, method: XHttpRequestOverride ? "POST" : Method.ToString(), data: formData);
                    break;

                case HttpRequestContentType.XML:
                    client.Headers.Add("Content-Type: application/xml");

                    if (values.Count != 1 || !IsPrimitive(values.First().Value))
                    {
                        throw new ArgumentException();
                    }

                    XmlSerializer             ser    = new XmlSerializer(values.First().Value.GetType());
                    System.Text.StringBuilder sb     = new System.Text.StringBuilder();
                    System.IO.StringWriter    writer = new System.IO.StringWriter(sb);
                    ser.Serialize(writer, values.First().Value);
                    response = client.UploadData(url, XHttpRequestOverride ? "POST" : Method.ToString(), Encoding.UTF8.GetBytes(values.First().Value as string));
                    break;

                case HttpRequestContentType.Bytes:
                    client.Headers.Add("Content-Type: application/octet-stream");

                    if (values.Count != 1 || !(values.First().Value is byte[]))
                    {
                        throw new ArgumentException();
                    }

                    response = client.UploadData(url, XHttpRequestOverride ? "POST" : Method.ToString(), values.First().Value as byte[]);
                    break;

                case HttpRequestContentType.String:
                    client.Headers.Add("Content-Type: text/plain;charset=utf-8");

                    if (values.Count != 1 || !(values.First().Value is byte[]))
                    {
                        throw new ArgumentException();
                    }

                    response = client.UploadData(url, XHttpRequestOverride ? "POST" : Method.ToString(), Encoding.UTF8.GetBytes(values.First().Value as string));
                    break;

                default:
                    goto case HttpRequestContentType.JSON;
                }
            }
            return(response);
        }