示例#1
0
        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;
                    }
                }

#if USE_GZIP
                if (req.RequestGzip)
                {
                    request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
                }
#endif

                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 e)
                    {
                        response.Exception = e;
                        if (action != null)
                        {
                            action(response);
                        }
                    }
                }, request);
            }
            catch (Exception e)
            {
                response.Exception = e;
                if (action != null)
                {
                    action(response);
                }
            }

            lock (pendingRequests)
                pendingRequests.Add(response);
            return(response);
        }
示例#2
0
		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;
				}

#if USE_GZIP
				if (req.RequestGzip)
					request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
#endif

				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 e)
					{
						response.Exception = e;
						if (action != null)
							action(response);
					}
				}, request);
			}
			catch (Exception e)
			{
				response.Exception = e;
				if (action != null)
					action(response);
			}

			lock (pendingRequests) 
				pendingRequests.Add(response);
			return response;
		}
示例#3
0
        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)
                    {
#if WINRT
                        if (item.Key != "User-Agent")                         // TODO fix this => cannot set UserAgent in WinRT
                        {
                            request.Headers[item.Key] = item.Value;
                        }
#elif WPF
                        if (item.Key != "User-Agent")
                        {
                            request.Headers[item.Key] = item.Value;
                        }
                        else
                        {
                            request.UserAgent = item.Value;
                        }
#else
                        request.Headers[item.Key] = item.Value;
#endif
                    }
                }

#if USE_GZIP
                if (req.RequestGzip)
                {
                    request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
                }
#endif

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

            lock (pendingRequests)
                pendingRequests.Add(response);
            return(response);
        }
示例#4
0
		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;
				}

#if USE_GZIP
				if (req.RequestGzip)
					request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
#endif

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

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