public async Task <TResponse> PostJsonData <TRequest, TResponse>(Uri uri, TRequest postData, CancellationToken cancellationToken = default(CancellationToken)) { try { var jsonString = await PostJsonDataAndReturnStringAsync(uri, postData, cancellationToken); return(SerializerHelper.Create().FromJson <TResponse>(jsonString)); } catch (Exception ex) { throw ex; } }
public async Task <string> PostJsonDataAndReturnStringAsync <TRequest>(Uri uri, TRequest postData, CancellationToken cancellationToken = default(CancellationToken)) { try { var content = new StringContent(SerializerHelper.Create().ToJson <TRequest>(postData), Encoding.UTF8, "application/json"); var response = await PostResponseAsync(uri, content, cancellationToken); response.EnsureSuccessStatusCode(); return(await response.Content.ReadAsStringAsync()); } catch (Exception ex) { throw ex; } }
public async Task <T> GetAsync <T>(Uri uri, CancellationToken cancellationToken = default(CancellationToken)) { try { var response = await GetResponseAsync(uri, cancellationToken); response.EnsureSuccessStatusCode(); string jsonString = await response.Content.ReadAsStringAsync(); return(SerializerHelper.Create().FromJson <T>(jsonString)); } catch (Exception ex) { throw ex; } }
public async Task <T> PostFormDataAsync <T>(Uri uri, List <KeyValuePair <string, string> > httpContent, CancellationToken cancellationToken = default(CancellationToken)) { try { var response = await PostResponseAsync(uri, new FormUrlEncodedContent(httpContent), cancellationToken); response.EnsureSuccessStatusCode(); // Sometimes the response should be decoded by the specific encoding. //var buffer = await response.Content.ReadAsBufferAsync(); //var byteArray = buffer.ToArray(); //var jsonString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length); string jsonString = await response.Content.ReadAsStringAsync(); return(SerializerHelper.Create().FromJson <T>(jsonString)); } catch (Exception ex) { throw ex; } }