Exemplo n.º 1
0
        /// <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;
        }
Exemplo n.º 2
0
 /// <summary>
 /// Issues a DELETE request against the service using a given path.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <returns>The <see cref="ResponseMessage"/>.</returns>
 public ResponseMessage Delete(string path) 
 {
     RequestMessage request = new RequestMessage("DELETE");
     return this.Send(path, request);
 }
Exemplo n.º 3
0
     Delete(string path, Dictionary<string, object> args) 
 {
     if (Count(args) > 0) 
     {
         path = path + "?" + Args.Encode(args);
     }
     RequestMessage request = new RequestMessage("DELETE");
     return this.Send(path, request);
 }
Exemplo n.º 4
0
     Post(string path, Dictionary<string, object> args) 
 {
     RequestMessage request = new RequestMessage("POST");
     if (Count(args) > 0) 
     {
         request.Content = Args.Encode(args);
     }
     return this.Send(path, request);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Issues an HTTP GET request against the service using a given path
 /// and arguments.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="args">The arguments.</param>
 /// <returns>The ResponseMessage.</returns>
 public ResponseMessage Get(string path, Dictionary<string, object> args) 
 {
     if (Count(args) > 0) 
     {
         path = path + "?" + Args.Encode(args);
     }
     RequestMessage request = new RequestMessage("GET");
     return this.Send(path, request);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Overloaded. Issues an HTTP request against the service using a 
 /// request path and message. 
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="request">The request message.</param>
 /// <returns>A <see cref="ResponseMessage"/>.</returns>
 /// <remarks>
 /// This method overrides the base 
 /// <see cref="HttpService"/>.<see cref="HttpService.Send"/> 
 /// method and applies the Splunk authorization header, which is 
 /// required for authenticated interactions with the Splunk service.
 /// </remarks>
 public override ResponseMessage Send(string path, RequestMessage request)
 {
     if (this.Token != null)
     {
         request.Header.Add("Authorization", this.Token);
     }
     return base.Send(this.Fullpath(path), request);
 }
Exemplo n.º 7
0
        /// <summary>
        /// Submits the data to the named index using variable arguments.
        /// </summary>
        /// <param name="indexName">The named index.</param>
        /// <param name="args">The variable arguments.</param>
        /// <param name="data">The data.</param>
        public void Submit(string indexName, Args args, string data)
        {
            string sendString = string.Empty;
            RequestMessage request = new RequestMessage("POST");
            request.Content = data;
            if (indexName != null)
            {
                sendString = string.Format("?index={0}", indexName);
            }

            if (args != null && args.Count > 0)
            {
                sendString = sendString + ((indexName == null) ? "?" : "&");
                sendString = sendString + args.Encode();
            }
            this.service.Send(
                this.service.SimpleReceiverEndPoint + sendString, request);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Overloaded. Issues an HTTP request against the service using a 
 /// request path and message. 
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="request">The request message.</param>
 /// <returns>A response message.</returns>
 /// <remarks>
 /// This method overrides the base 
 /// <see cref="HttpService"/>.<see cref="HttpService.Send"/> 
 /// method and applies the Splunk authorization header, which is 
 /// required for authenticated interactions with the Splunk service.
 /// </remarks>
 public override ResponseMessage Send(string path, RequestMessage request)
 {
     //this.SimpleReceiverEndPoint = "receivers/simple";
     //this.PasswordEndPoint = "admin/changeme";
     //this.App = null;
     //this.Owner = "admin";
     //this.Password = "******";
     //this.Token = "Basic YWRtaW46Y2hhbmdlbWU=";
     //this.Username = "******";
     //this.Version = "6.0";
     //// this.Token = "Basic YWRtaW46Y2hhbmdlbWU=";
     if (this.Token != null)
     {
         request.Header.Add("Authorization", this.Token);
     }
     return base.Send(this.Fullpath(path), request);
 }