Beispiel #1
0
        protected async Task <T> Post <T>(string path, JObject obj)
        {
            var requestStream = MemoryManager.GetMemoryStream("HttpConnection.Post");

            try {
                StdJson.ObjectToStream(obj, requestStream);
                requestStream.Seek(0, SeekOrigin.Begin);
            }
            catch (Exception) {
                requestStream.Dispose();
                throw;
            }

            HttpResponseMessage response = null;

            using (var payload = new StreamContent(requestStream)) {
                try {
                    response = await client.PostAsync(path, payload);
                }
                catch (Exception exp) {
                    OnConnectionBroken();
                    throw new ConnectivityException(exp.Message, exp);
                }
            }

            using (response) {
                if (response.IsSuccessStatusCode)
                {
                    try {
                        Stream stream = await response.Content.ReadAsStreamAsync();

                        using (var reader = new StreamReader(stream, Encoding.UTF8)) {
                            return(StdJson.ObjectFromReader <T>(reader));
                        }
                    }
                    catch (Exception exp) {
                        OnConnectionBroken();
                        throw new ConnectivityException(exp.Message, exp);
                    }
                }
                else
                {
                    await ThrowError(response);

                    return(default(T)); // never come here
                }
            }
        }
Beispiel #2
0
 public static T ObjectFromUtf8Stream <T>(Stream stream)
 {
     using (var reader = new StreamReader(stream, Encoding.UTF8)) {
         return(StdJson.ObjectFromReader <T>(reader));
     }
 }