Exemple #1
0
        public Dictionary <string, string> PostAndReturnHeaders <T>(string aiRessource, T aiRequestBody)
        {
            Dictionary <string, string> aoDic = new Dictionary <string, string>();

            using (MemoryStream ms = new MemoryStream())
            {
                SerializationContainer.JsonSerializer.WriteObject(ms, aiRequestBody);
                byte[] requestInJson = ms.ToArray();
                HttpCompletionOption       option   = HttpCompletionOption.ResponseContentRead;
                HttpRequestMessage         request  = AddBasicAuthentication(HttpClientUtils.CreateBlankRequestMessage(HttpMethod.Post, aiRessource).AddContentToRequest(aiRequestBody));
                Task <HttpResponseMessage> response = _httpClient.SendAsync(request, option);
                try
                {
                    HttpResponseMessage message = response.Result;
                    message.EnsureSuccessStatusCode();
                    foreach (var header in message.Headers)
                    {
                        aoDic.Add(header.Key, header.Value.First());
                    }
                }
                catch (Exception e)
                {
                    //ToDo: Logging.
                    return(null);
                }
            }
            return(aoDic);
        }
Exemple #2
0
        public HttpStatusCode Put(string aiRessource, string aiQuery)
        {
            HttpCompletionOption       option   = HttpCompletionOption.ResponseContentRead;
            HttpRequestMessage         request  = AddBasicAuthentication(HttpClientUtils.CreateBlankRequestMessage(HttpMethod.Put, aiRessource));
            Task <HttpResponseMessage> response = _httpClient.SendAsync(request, option);
            HttpResponseMessage        message  = response.Result;

            return(message.StatusCode);
        }
Exemple #3
0
        public Stream GetRaw(string aiRessource)
        {
            HttpRequestMessage         request  = AddBasicAuthentication(HttpClientUtils.CreateBlankRequestMessage(HttpMethod.Get, aiRessource));
            HttpCompletionOption       option   = HttpCompletionOption.ResponseContentRead;
            Task <HttpResponseMessage> response = _httpClient.SendAsync(request, option);
            HttpResponseMessage        message  = response.Result;

            return(message.Content.ReadAsStreamAsync().Result);
        }
Exemple #4
0
        public HttpStatusCode Put(string aiRessource, Dictionary <string, string> aiHeaders)
        {
            HttpCompletionOption option  = HttpCompletionOption.ResponseContentRead;
            HttpRequestMessage   request = AddBasicAuthentication(HttpClientUtils.CreateBlankRequestMessage(HttpMethod.Put, aiRessource));

            foreach (var header in aiHeaders)
            {
                request.Headers.Add(header.Key, header.Value);
            }
            Task <HttpResponseMessage> response = _httpClient.SendAsync(request, option);
            HttpResponseMessage        message  = response.Result;

            return(message.StatusCode);
        }
Exemple #5
0
 public HttpStatusCode PostMultiparts(string aiRessource, IList <HttpContent> aiContentList, out string aoMessage)
 {
     using (var multiPartStream = new MultipartFormDataContent())
     {
         foreach (var content in aiContentList)
         {
             multiPartStream.Add(content);
         }
         HttpRequestMessage requestMessage = AddBasicAuthentication(HttpClientUtils.CreateBlankRequestMessage(HttpMethod.Post, aiRessource));
         requestMessage.Content = multiPartStream;
         HttpCompletionOption       option   = HttpCompletionOption.ResponseContentRead;
         Task <HttpResponseMessage> response = _httpClient.SendAsync(requestMessage, option);
         HttpResponseMessage        message  = response.Result;
         aoMessage = message.Content.ReadAsStringAsync().Result;//Save content temporarly and return it.
         return(message.StatusCode);
     }
 }
Exemple #6
0
        public T Get <T>(string aiRessource)
        {
            T obj = default(T);
            HttpCompletionOption       option   = HttpCompletionOption.ResponseContentRead;
            HttpRequestMessage         request  = AddBasicAuthentication(HttpClientUtils.CreateBlankRequestMessage(HttpMethod.Get, aiRessource));
            Task <HttpResponseMessage> response = _httpClient.SendAsync(request, option);
            HttpResponseMessage        message  = response.Result;

            if (message.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new UnauthorizedAccessException(String.Format(Messages.Errors.Not_Authorized, UserName));
            }
            message.EnsureSuccessStatusCode();
            Task <Stream> result = message.Content.ReadAsStreamAsync();

            obj = SerializationContainer.JsonSerializer.ReadObject <T>(result.Result);
            return(obj);
        }
Exemple #7
0
        public R Post <T, R>(string aiRessource, T aiRequestBody)
        {
            R obj = default(R);

            using (MemoryStream ms = new MemoryStream())
            {
                SerializationContainer.JsonSerializer.WriteObject(ms, aiRequestBody);                byte[] requestInJson = ms.ToArray();
                HttpCompletionOption       option   = HttpCompletionOption.ResponseContentRead;
                HttpRequestMessage         request  = AddBasicAuthentication(HttpClientUtils.CreateBlankRequestMessage(HttpMethod.Post, aiRessource).AddContentToRequest(aiRequestBody));
                Task <HttpResponseMessage> response = _httpClient.SendAsync(request, option);
                HttpResponseMessage        message  = response.Result;
                string contentForDebug = message.Content.ReadAsStringAsync().Result;//Save content temporarly for messaging-process.
                message.EnsureSuccessStatusCode();
                if (message.Content != null)
                {
                    var result = message.Content.ReadAsStreamAsync();
                    obj = SerializationContainer.JsonSerializer.ReadObject <R>(result.Result);
                }
            }
            return(obj);
        }