Example #1
0
        private static void TestB2()
        {
            Energy.Base.Collection.StringDictionary d = new Energy.Base.Collection.StringDictionary();
            d.CaseSensitive = true;
            Console.WriteLine("Setting \"Option\", \"option\" and \"OPTION\" to different values");
            d["Option"] = "Value1";
            d["option"] = "Value2";
            d["OPTION"] = "Value3";
            string value;

            value = d.Get("option");
            Console.WriteLine("Value of \"option\" when CaseSensitive is true: {0}", value);
            d.CaseSensitive = false;
            value           = d.Get("option");
            Console.WriteLine("Value of \"option\" when CaseSensitive is false: {0}", value);
            d.CaseSensitive = true;
            Energy.Core.Tilde.WriteLine("~y~Array when CaseSensitive is true");
            Console.WriteLine(string.Join(", ", d.ToArray("=")));
            d.CaseSensitive = false;
            Energy.Core.Tilde.WriteLine("~y~Array when CaseSensitive is false");
            Console.WriteLine(string.Join(", ", d.ToArray("=")));
        }
Example #2
0
        public static int Request(string method, string url, byte[] data
                                  , string contentType
                                  , string acceptType
                                  , string[] requestHeaders
                                  , out string[] responseHeaders
                                  , out byte[] responseData
                                  )
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = method;
            if (!string.IsNullOrEmpty(contentType))
            {
                request.ContentType = contentType;
            }
            if (!string.IsNullOrEmpty(acceptType))
            {
                request.Accept = acceptType;
            }

            if (requestHeaders != null && requestHeaders.Length > 0)
            {
                //for (int i = 0; i < requestHeaders.Length / 2; i++)
                //{
                //    request.Headers.Add(requestHeaders[i], requestHeaders[i + 1]);
                //}
                for (int i = 0; i < requestHeaders.Length; i++)
                {
                    request.Headers.Add(requestHeaders[i]);
                }
            }

            int statusCode = 0;

            responseHeaders = null;
            responseData    = null;

            if (data != null && data.Length > 0)
            {
                request.ContentLength = data.Length;

                using (Stream requestStream = request.GetRequestStream())
                {
                    if (requestStream == null)
                    {
                        return(0);
                    }

                    requestStream.Write(data, 0, data.Length);
                    requestStream.Flush();
                    requestStream.Close();
                }
            }

            using (HttpWebResponse response = (HttpWebResponse)GetResponseWithoutException(request))
            {
                statusCode = (int)response.StatusCode;
                if (response.Headers.Count > 0)
                {
                    Energy.Base.Collection.StringDictionary d = new Energy.Base.Collection.StringDictionary();
                    string[] keys = response.Headers.AllKeys;
                    for (int i = 0; i < keys.Length; i++)
                    {
                        d[keys[i]] = response.Headers[i];
                    }
                    responseHeaders = d.ToArray();
                }
                using (Stream responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                    {
                        using (Energy.Base.ByteArrayBuilder builder = new Energy.Base.ByteArrayBuilder(responseStream))
                        {
                            responseData = builder.ToArray();
                        }
                    }
                    return(statusCode);
                }
            }
        }