コード例 #1
0
ファイル: Http.cs プロジェクト: yjlintw/YJToolkit
        private static HttpResponse Post(HttpPostRequest req, Action<HttpResponse> action, string method)
        {
            var response = new HttpResponse(req);

            try
            {
                var boundary = "";
                var request = CreateRequest(req);

                if (req.Credentials != null)
                {
                    request.Credentials = req.Credentials;
                }

                response.WebRequest = request;

                if (req.Files.Count == 0)
                {
                    request.ContentType = "application/x-www-form-urlencoded";
                }
                else
                {
                    boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
                    request.ContentType = "multipart/form-data; boundary=" + boundary;
                }

                if (req.Cookies.Count > 0)
                {
                    request.CookieContainer = new CookieContainer();
                    foreach (var c in req.Cookies)
                    {
                        request.CookieContainer.Add(request.RequestUri, c);
                    }
                }

                request.Method = method;
                if (req.ContentType != null)
                {
                    request.ContentType = req.ContentType;
                }

                if (req.Headers.Count > 0)
                {
                    foreach (var item in req.Headers)
                    {
                        request.Headers[item.Key] = item.Value;
                    }
                }

                response.CreateTimeoutTimer(request);
                request.BeginGetRequestStream(delegate(IAsyncResult ar1)
                {
                    try
                    {
                        using (var stream = request.EndGetRequestStream(ar1))
                        {
                            if (req.Files.Count > 0)
                            {
                                WritePostData(stream, boundary, req);
                            }
                            else
                            {
                                WritePostData(stream, req);
                            }
                        }
                        request.BeginGetResponse(r => ProcessResponse(r, request, response, action), request);
                    }
                    catch (Exception ex)
                    {
                        response.Exception = ex;
                        if (action != null)
                            action(response);
                    }
                }, request);
            }
            catch (System.Exception ex)
            {
                response.Exception = ex;
                if (action != null)
                {
                    action(response);
                }
            }

            lock (pendingRequests)
            {
                pendingRequests.Add(response);
            }

            return response;
        }
コード例 #2
0
ファイル: Http.cs プロジェクト: yjlintw/YJToolkit
        public static HttpResponse Get(HttpGetRequest req, Action<HttpResponse> action)
        {
            var response = new HttpResponse(req);
            try
            {
                if (!req.UseCache)
                {
                    req.Query["__dcachetime"] = DateTime.Now.Ticks.ToString();
                }

                var request = CreateRequest(req);

                if (req.Credentials != null)
                {
                    request.Credentials = req.Credentials;
                }
                response.WebRequest = request;

                if (req.Cookies.Count > 0)
                {
                    request.CookieContainer = new CookieContainer();
                    foreach (var c in req.Cookies)
                    {
                        request.CookieContainer.Add(request.RequestUri, c);
                    }
                }

                request.Method = "GET";
                if (req.ContentType != null)
                {
                    request.ContentType = req.ContentType;
                }

                if (req.Headers.Count > 0)
                {
                    foreach (var item in req.Headers)
                    {
                        request.Headers[item.Key] = item.Value;
                    }
                }

                response.CreateTimeoutTimer(request);
                request.BeginGetRequestStream(r => ProcessResponse(r, request, response, action), request);
            }
            catch (Exception ex)
            {
                response.Exception = ex;
                if (action != null)
                {
                    action(response);
                }
            }

            lock (pendingRequests)
            {
                pendingRequests.Add(response);
            }

            return response;
        }