Exemple #1
0
        /// <summary>This function converts a dictionary to a JSON string and posts it as content to a website
        /// and then returns a JSONWebResponse object to the user with the content, statuscode, and any Exception.</summary>
        /// <param name="url">url where to post the data</param>
        /// <param name="dictionay">Dictionary<string,string> of key value to send to url</param>
        /// <param name="sendAsJSON">If set to true sends as JSON, otherwise sends as posted form fields</param>
        /// <returns>A JSONWebResponse object</returns>
        public static JSONWebResponse DictionaryToUrl(string url, Dictionary <string, string> dictionay)
        {
            try
            {
                //Set up the web request
                HttpWebRequest request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
                request.Method      = "POST";
                request.ContentType = "application/json";

                //Convert the Dictionary to JSON
                string jsondata = DictionaryToJSON(dictionay);

                // Encode the parameters as content data, and set the content length
                byte[] JSONContent = UTF8Encoding.UTF8.GetBytes(jsondata);
                request.ContentLength = JSONContent.Length;

                // Send the request:
                using (Stream post = request.GetRequestStream())
                {
                    post.Write(JSONContent, 0, JSONContent.Length);
                }

                // Pick up the response
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());

                    JSONWebResponse webresponse = new JSONWebResponse()
                    {
                        StatusCode   = response.StatusCode,
                        Content      = reader.ReadToEnd(),
                        ErrorDetails = null
                    };

                    return(webresponse);
                }
            }
            catch (Exception ex)
            {
                JSONWebResponse response = new JSONWebResponse()
                {
                    StatusCode   = HttpStatusCode.InternalServerError,
                    Content      = "Error Retrieving Data",
                    ErrorDetails = ex
                };

                return(response);
            }
        }
Exemple #2
0
        /// <summary>
        /// This function takes a parser and options from the CommandLion Parser Nuget and the
        /// args sent by the user and tries to post the inputs as JSON to a website using
        /// the PostJSON class
        /// </summary>
        public static void PostOptions(Parser parser, Options options, string[] args)
        {
            //Try to parse the args into the options variable
            if (parser.ParseArguments(args, options))
            {
                Dictionary <string, string> JSONData = new Dictionary <string, string>();

                //Check to see if the Type is 2Elements of ElementArray
                // *************** TYPE: 2Elements ***************
                if (options.InputType == "2Elements")
                {
                    JSONData.Add(options.ContentName, options.Content);

                    if (options.Content2 != null && options.ContentName2 != null)
                    {
                        JSONData.Add(options.ContentName2, options.Content2);
                    }
                }
                // *************** TYPE: Element Array ***************
                else //If the input is an ElementArray Do This
                {
                    string[] NameList  = options.NameList.Split('|');
                    string[] ValueList = options.ValueList.Split('|');

                    if (NameList.Count() != ValueList.Count() || NameList.Count() == 0 || ValueList.Count() == 0)
                    {
                        //If they didn't enter valid args show the description
                        Console.WriteLine(options.GetUsage());
                        return;
                    }

                    for (int i = 0; i < NameList.Count(); i++)
                    {
                        JSONData.Add(NameList[i], ValueList[i]);
                    }
                }

                //*************** Send Data *******************
                // consume Options type properties
                if (options.Verbose)
                {
                    Console.WriteLine("Sending...");
                    Console.WriteLine("");

                    //Post the JSON data to the URL
                    JSONWebResponse response = PostJSON.DictionaryToUrl(options.url, JSONData);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Console.WriteLine("Sent Successfully!");
                        Console.WriteLine("");
                        if (response.Content != null)
                        {
                            Console.WriteLine("Data Returned:");
                            Console.WriteLine(response.Content);
                            Console.WriteLine("");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error Code: " + response.StatusCode.ToString());
                        Console.WriteLine("");

                        if (response.Content != null)
                        {
                            Console.WriteLine("Data Returned:");
                            Console.WriteLine(response.Content);
                            Console.WriteLine("");
                        }

                        if (response.ErrorDetails != null && response.ErrorDetails.Message != null)
                        {
                            Console.WriteLine("Details:");
                            Console.WriteLine(response.ErrorDetails.Message);
                            Console.WriteLine("");
                        }
                    }
                }
                else
                {
                    JSONWebResponse response = PostJSON.DictionaryToUrl(options.url, JSONData);
                }
            }
            else
            {
                //If they didn't enter valid args show the description
                Console.WriteLine(options.GetUsage());
            }
        }