コード例 #1
0
        public static void httpTo(bool async, string method, string contentType, string url, IDictionary <string, object> parms, IDictionary <string, object> headers, HttpCallbackSuccess success, HttpCallbackFail fail)
        {
            string                str;
            Exception             exception;
            Func <string, string> selector = null;
            Func <string, string> func2    = null;
            AsyncCallback         callback = null;

            if (ServicePointManager.DefaultConnectionLimit < 200)
            {
                ServicePointManager.DefaultConnectionLimit = 200;
            }
            if ((method.ToUpper() == "GET") && ((parms != null) && (parms.Count > 0)))
            {
                if (selector == null)
                {
                    selector = item => HttpUtility.UrlEncode((item + "=" + parms[item]) ?? "");
                }
                str = string.Join("&", parms.Keys.Select <string, string>(selector));
                if (url.IndexOf("?") != -1)
                {
                    url = url + "?" + str;
                }
                else
                {
                    url = url + "&" + str;
                }
            }
            GC.Collect();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method    = method;
            request.KeepAlive = false;
            request.Timeout   = 0x7530;
            if ((headers != null) && (headers.Count > 0))
            {
                foreach (string str2 in headers.Keys)
                {
                    request.Headers.Add(str2, Convert.ToString(headers[str2]));
                }
            }
            if (method.ToUpper() == "POST")
            {
                request.ContentType = contentType;
                if ((parms != null) && (parms.Count > 0))
                {
                    byte[] bytes = null;
                    if (contentType.ToLower() == "application/x-www-form-urlencoded")
                    {
                        if (func2 == null)
                        {
                            func2 = item => ((item + "=" + parms[item]) ?? "");
                        }
                        str   = string.Join("&", parms.Keys.Select <string, string>(func2));
                        bytes = Encoding.UTF8.GetBytes(str);
                    }
                    else if (contentType.ToLower() == "application/json")
                    {
                        bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(parms));
                    }
                    if ((bytes != null) && (bytes.Length > 0))
                    {
                        Stream requestStream = request.GetRequestStream();
                        requestStream.Write(bytes, 0, bytes.Length);
                        requestStream.Close();
                    }
                }
            }
            if (async)
            {
                try
                {
                    if (callback == null)
                    {
                        callback = delegate(IAsyncResult item)
                        {
                            using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(item))
                            {
                                if (response.StatusCode == HttpStatusCode.OK)
                                {
                                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                                    {
                                        if (response.ContentType.Contains("application/json"))
                                        {
                                            success(JsonConvert.DeserializeObject(reader.ReadToEnd()), response);
                                        }
                                        else
                                        {
                                            success(reader.ReadToEnd(), response);
                                        }
                                    }
                                }
                                else if (fail != null)
                                {
                                    fail(new HttpException((int)response.StatusCode, "请求错误"));
                                }
                            }
                        };
                    }
                    request.BeginGetResponse(callback, null);
                }
                catch (Exception exception1)
                {
                    exception = exception1;
                    if (fail != null)
                    {
                        fail(new HttpException(exception.Message, exception));
                    }
                    if (request != null)
                    {
                        request.Abort();
                    }
                }
            }
            else
            {
                try
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                            {
                                if (response.ContentType.Contains("application/json"))
                                {
                                    success(JsonConvert.DeserializeObject(reader.ReadToEnd()), response);
                                }
                                else
                                {
                                    success(reader.ReadToEnd(), response);
                                }
                            }
                        }
                        else if (fail != null)
                        {
                            fail(new HttpException((int)response.StatusCode, "请求错误"));
                        }
                    }
                }
                catch (Exception exception2)
                {
                    exception = exception2;
                    if (fail != null)
                    {
                        fail(new HttpException(exception.Message, exception));
                    }
                }
                finally
                {
                    if (request != null)
                    {
                        request.Abort();
                    }
                }
            }
        }
コード例 #2
0
 public static void postSyncAsForm(string url, IDictionary <string, object> parms, IDictionary <string, object> headers, HttpCallbackSuccess success)
 {
     httpTo(false, "POST", "application/x-www-form-urlencoded", url, parms, headers, success, null);
 }
コード例 #3
0
 public static void getSyncTo(string url, IDictionary <string, object> parms, IDictionary <string, object> headers, HttpCallbackSuccess success)
 {
     httpTo(false, "GET", null, url, parms, headers, success, null);
 }
コード例 #4
0
 public static void postSyncAsForm(string url, IDictionary <string, object> parms, HttpCallbackSuccess success)
 {
     postSyncAsForm(url, parms, null, success);
 }
コード例 #5
0
 public static void getSyncTo(string url, HttpCallbackSuccess success)
 {
     getSyncTo(url, null, null, success);
 }