/// <summary> /// The main HTTP send method. Sends any of the supported HTTP/S /// methods to the service. /// </summary> /// <param name="path">The path.</param> /// <param name="request">The requestMessage.</param> /// <returns>The responseMessage.</returns> public virtual ResponseMessage Send(string path, RequestMessage request) { // Construct a full URL to the resource Uri url = this.GetUrl(path); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); // build web request. webRequest.Method = request.Method; webRequest.AllowAutoRedirect = true; // Add headers from request message Dictionary<string, string> header = request.Header; foreach (KeyValuePair<string, string> entry in header) { webRequest.Headers.Add(entry.Key, entry.Value); } // Reflection can be expensive, thus cache userAgent value. if (userAgent == null) { var assembly = Assembly.GetExecutingAssembly(); // Use file version since it is common for it to change without // an assembly version change. var fvi = FileVersionInfo.GetVersionInfo(assembly.Location); var version = fvi.FileVersion; userAgent = "splunk-sdk-csharp/" + version; } webRequest.UserAgent = userAgent; webRequest.Accept = "*/*"; if (request.Method.Equals("POST")) { webRequest.ContentType = "application/x-www-form-urlencoded"; } // Write out request content, if any object content = request.Content; if (content != null) { // Get the bytes for proper encoded length when going over the // wire. byte[] bytes = Encoding.UTF8.GetBytes((string)content); webRequest.ContentLength = bytes.GetLength(0); using (var stream = webRequest.GetRequestStream()) // Default encoding of StreamWriter is UTF-8. using (var streamWriter = new StreamWriter(stream)) { streamWriter.Write(content); streamWriter.Flush(); } } int status; HttpWebResponse response = null; try { response = (HttpWebResponse)webRequest.GetResponse(); status = response.StatusCode.GetHashCode(); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) { var resp = (HttpWebResponse)ex.Response; status = resp.StatusCode.GetHashCode(); } throw; } Stream input; input = status >= 400 ? null : response != null ? response.GetResponseStream() : null; // If there is no input, then we can closeout this response // straight away. if (input == null) { response.Close(); response = null; } ResponseMessage returnResponse = new ResponseMessage(status, input, response); return returnResponse; }
internal ResponseStream(ResponseMessage message) { this.message = message; }
/// <summary> /// Returns this job's SID from within a response message, as opposed /// to directly from the <see cref="Job"/> object. /// </summary> /// <param name="response">Response message.</param> /// <returns>The job's SID.</returns> public static string SidExtraction(ResponseMessage response) { StreamReader streamReader = new StreamReader(response.Content); XmlDocument doc = new XmlDocument(); string foo = streamReader.ReadToEnd(); doc.LoadXml(foo); //doc.LoadXml(streamReader.ReadToEnd()); return doc.SelectSingleNode("/response/sid").InnerText; }
/// <summary> /// Checks to make sure response is an HTTP OK. /// </summary> /// <param name="response">The repsonse message</param> private void CheckResponse(ResponseMessage response) { Assert.AreEqual(200, response.Status, this.assertRoot + "#2"); try { // Make sure we can at least load the Atom response AtomFeed.Parse(response.Content); } catch (Exception e) { Assert.Fail(e.Message); } }
internal ResponseStream(ResponseMessage message, bool isExportResult) { this.message = message; this.isExportResult = isExportResult; }
internal ResponseStream(ResponseMessage message) { this.message = message; }