Ejemplo n.º 1
0
        private void Send(string method, string url, string body, string contentType, UserCredentials userCredentials,
                          Action <HttpResponse> onSuccess, Action <Exception> onException)
        {
            var request = new HttpRequestMessage();

            request.RequestUri = new Uri(url);
            request.Method     = new System.Net.Http.HttpMethod(method);

            if (userCredentials != null)
            {
                AddAuthenticationHeader(request, userCredentials);
            }

            var bodyBytes = UTF8NoBom.GetBytes(body);
            var stream    = new MemoryStream(bodyBytes);
            var content   = new StreamContent(stream);

            content.Headers.ContentType   = new MediaTypeHeaderValue(contentType);
            content.Headers.ContentLength = bodyBytes.Length;

            request.Content = content;

            var state = new ClientOperationState(request, onSuccess, onException);

            _client.SendAsync(request).ContinueWith(RequestSent(state));
        }
Ejemplo n.º 2
0
        private void Receive(string method, string url, UserCredentials userCredentials,
                             Action <HttpResponse> onSuccess, Action <Exception> onException, string hostHeader = "")
        {
            var request = new HttpRequestMessage();

            request.RequestUri = new Uri(url);
            request.Method     = new System.Net.Http.HttpMethod(method);

            if (userCredentials != null)
            {
                AddAuthenticationHeader(request, userCredentials);
            }

            if (_enableVersion5Compability)
            {
                hostHeader = "";
            }

            if (!string.IsNullOrWhiteSpace(hostHeader))
            {
                request.Headers.Host = hostHeader;
            }

            var state = new ClientOperationState(request, onSuccess, onException);

            _client.SendAsync(request).ContinueWith(RequestSent(state));
        }
Ejemplo n.º 3
0
        private void Receive(string method, string url, UserCredentials userCredentials,
                             Action <HttpResponse> onSuccess, Action <Exception> onException, string hostHeader = "")
        {
            var request = new HttpRequestMessage();

//MONOCHECK IS THIS STILL NEEDED?
#if MONO
            request.Headers.Add("Keep-alive", "false");
#endif
            request.RequestUri = new Uri(url);
            request.Method     = new System.Net.Http.HttpMethod(method);

            if (userCredentials != null)
            {
                AddAuthenticationHeader(request, userCredentials);
            }

            if (!string.IsNullOrWhiteSpace(hostHeader))
            {
                request.Headers.Host = hostHeader;
            }

            var state = new ClientOperationState(request, onSuccess, onException);
            _client.SendAsync(request).ContinueWith(RequestSent(state));
        }
Ejemplo n.º 4
0
        private void Send(string method, string url, string body, string contentType, UserCredentials userCredentials, TimeSpan timeout,
                          Action <HttpResponse> onSuccess, Action <Exception> onException)
        {
            var request   = (HttpWebRequest)WebRequest.Create(url);
            var bodyBytes = UTF8NoBom.GetBytes(body);

            request.Method        = method;
            request.KeepAlive     = true;
            request.Pipelined     = true;
            request.ContentLength = bodyBytes.Length;
            request.ContentType   = contentType;
            if (userCredentials != null)
            {
                AddAuthenticationHeader(request, userCredentials);
            }

            var state = new ClientOperationState(_log, request, onSuccess, onException);

            state.InputStream = new MemoryStream(bodyBytes);

            var result = request.BeginGetRequestStream(GotRequestStream, state);

            ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, TimeoutCallback, request,
                                                   (int)timeout.TotalMilliseconds, true);
        }
Ejemplo n.º 5
0
 private Action <Task <string> > ResponseRead(ClientOperationState state)
 {
     return(task => {
         try {
             state.Response.Body = task.Result;
             state.Dispose();
             state.OnSuccess(state.Response);
         } catch (Exception ex) {
             state.Dispose();
             state.OnError(ex);
         }
     });
 }
Ejemplo n.º 6
0
 private Action <Task <HttpResponseMessage> > RequestSent(ClientOperationState state)
 {
     return(task => {
         try {
             var responseMsg = task.Result;
             state.Response = new HttpResponse(responseMsg);
             responseMsg.Content.ReadAsStringAsync()
             .ContinueWith(ResponseRead(state));
         } catch (Exception ex) {
             state.Dispose();
             state.OnError(ex);
         }
     });
 }
Ejemplo n.º 7
0
        private void Send(string method, string url, string body, string contentType, Action <HttpResponse> onSuccess, Action <Exception> onException)
        {
            var request   = (HttpWebRequest)WebRequest.Create(url);
            var bodyBytes = Encoding.UTF8.GetBytes(body);

            request.Method        = method;
            request.KeepAlive     = false;//TODO TR : hangs on mono
            request.Pipelined     = false;
            request.ContentLength = bodyBytes.Length;
            request.ContentType   = contentType;

            var state = new ClientOperationState(request, onSuccess, onException)
            {
                InputStream = new MemoryStream(bodyBytes)
            };

            request.BeginGetRequestStream(GotRequestStream, state);
        }
Ejemplo n.º 8
0
        private void Send(string method, string url, string body, string contentType, Action<HttpResponse> onSuccess, Action<Exception> onException)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            var bodyBytes = Encoding.UTF8.GetBytes(body);

            request.Method = method;
            request.KeepAlive = false;//TODO TR : hangs on mono
            request.Pipelined = false;
            request.ContentLength = bodyBytes.Length;
            request.ContentType = contentType;

            var state = new ClientOperationState(request, onSuccess, onException)
            {
                InputStream = new MemoryStream(bodyBytes)
            };
            request.BeginGetRequestStream(GotRequestStream, state);
        }
Ejemplo n.º 9
0
        private void Send(string method, string url, string body, string contentType, UserCredentials userCredentials, TimeSpan timeout,
                          Action<HttpResponse> onSuccess, Action<Exception> onException)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            var bodyBytes = UTF8NoBom.GetBytes(body);

            request.Method = method;
            request.KeepAlive = true;
            request.Pipelined = true;
            request.ContentLength = bodyBytes.Length;
            request.ContentType = contentType;
            if (userCredentials != null)
                AddAuthenticationHeader(request, userCredentials);

            var state = new ClientOperationState(_log, request, onSuccess, onException);
            state.InputStream = new MemoryStream(bodyBytes);

            var result = request.BeginGetRequestStream(GotRequestStream, state);
            ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, TimeoutCallback, request,
                                                   (int) timeout.TotalMilliseconds, true);
        }
Ejemplo n.º 10
0
        private void Send(string method, string url, string body, string contentType, UserCredentials userCredentials,
                          Action<HttpResponse> onSuccess, Action<Exception> onException)
        {
            var request = new HttpRequestMessage();
            request.RequestUri = new Uri(url);
            request.Method = new System.Net.Http.HttpMethod(method);

            if (userCredentials != null)
                AddAuthenticationHeader(request, userCredentials);

            var bodyBytes = UTF8NoBom.GetBytes(body);
            var stream = new MemoryStream(bodyBytes);
            var content = new StreamContent(stream);
            content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
            content.Headers.ContentLength = bodyBytes.Length;

            request.Content = content;

            var state = new ClientOperationState(request, onSuccess, onException);
            _client.SendAsync(request).ContinueWith(RequestSent(state));
        }
Ejemplo n.º 11
0
        private void Receive(string method, string url, UserCredentials userCredentials,
                             Action<HttpResponse> onSuccess, Action<Exception> onException, string hostHeader = "")
        {
            var request = new HttpRequestMessage();
#if __MonoCS__
            request.Headers.Add("Keep-alive", "false");
#endif
            request.RequestUri = new Uri(url);
            request.Method = new System.Net.Http.HttpMethod(method);

            if(userCredentials != null)
                AddAuthenticationHeader(request, userCredentials);

            if (!string.IsNullOrWhiteSpace(hostHeader))
                request.Headers.Host = hostHeader;

            var state = new ClientOperationState(request, onSuccess, onException);
            _client.SendAsync(request).ContinueWith(RequestSent(state));
        }
Ejemplo n.º 12
0
 private Action<Task<string>> ResponseRead(ClientOperationState state) 
 {
     return task => {
         try 
         {
             state.Response.Body = task.Result;
             state.Dispose();
             state.OnSuccess(state.Response);
         }
         catch(Exception ex)
         {
             state.Dispose();
             state.OnError(ex);
         }
     };
 }
Ejemplo n.º 13
0
 private Action<Task<HttpResponseMessage>> RequestSent(ClientOperationState state)
 {
     return task =>
     {
         try
         {
             var responseMsg = task.Result;
             state.Response = new HttpResponse(responseMsg);
             responseMsg.Content.ReadAsStringAsync()
                     .ContinueWith(ResponseRead(state));
         }
         catch (Exception ex)
         {
             state.Dispose();
             state.OnError(ex);
         }
     };
 }