protected override HttpWebRequest SetHeader(string url, METHOD method, Dictionary<string, string> headers) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ContentType = "application/x-www-form-urlencoded"; request.Accept = "application/json"; request.Method = method.ToString(); request.Timeout = 5000; System.Net.ServicePointManager.Expect100Continue = false; //to prevent from adding ExpectContinue: 100 to Header if (headers != null && headers.Count > 0) { foreach (KeyValuePair<string, string> pair in headers) request.Headers.Add(pair.Key, pair.Value); } return request; }
/// <summary> /// Makes a call to FluidDB using JSON payload /// </summary> /// <param name="method">The type of HTTP method</param> /// <param name="path">The path to call</param> /// <param name="body">The body of the request</param> /// <param name="args">Any further arguments to append to the URI</param> /// <returns>The raw result</returns> public HttpWebResponse Call(METHOD m, string path, Dictionary<string, string> args, Dictionary<string, object> body) { // Build the request Uri requestUri = ProcessURI(m, path, args); WebRequest request = WebRequest.Create(requestUri); request.Method = m.ToString(); // Make sure the headers are correct ((HttpWebRequest)request).UserAgent = ".NET FluidDB Client"; ((HttpWebRequest)request).Accept = "application/json"; if (!(this.password == string.Empty & this.username == string.Empty)) { string userpass = username + ":" + password; byte[] encUserPass = Encoding.UTF8.GetBytes(userpass); string auth = "Basic " + Convert.ToBase64String(encUserPass).Trim(); ((HttpWebRequest)request).Headers.Add(HttpRequestHeader.Authorization, auth); } if (body == null || body.Count == 0) { request.ContentType = "text/plain"; } else { Byte[] byteArray = Encoding.ASCII.GetBytes(this.jss.Serialize(body)); request.ContentType = "application/json"; request.ContentLength = byteArray.Length; Stream bodyStream = request.GetRequestStream(); bodyStream.Write(byteArray, 0, byteArray.Length); bodyStream.Close(); } // Call FluidDB try { return (HttpWebResponse)request.GetResponse(); } catch (WebException e) { // I don't want you to raise an exception dammit... I just want the raw // response and I'll process the errorClass from the content and maybe // raise an exception if appropriate elsewhere return (HttpWebResponse)e.Response; } }