示例#1
0
        public static async Task <HttpResponseMessage> PostAsStream(this HttpClient client,
                                                                    string requestUri, object value, JsonSerializerSettings serializerSettings, string bearerToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            var ms = new MemoryStream();

            using (var sw = new StreamWriter(ms, new UTF8Encoding(), 1024, true))
            {
                using (var tw = new JsonTextWriter(sw))
                {
                    var jsonSerializer = new JsonSerializer();
                    jsonSerializer.Serialize(tw, value);
                    tw.Flush();
                }
            }

            ms.Seek(0, SeekOrigin.Begin);

            using (var streamContent = new StreamContent(ms))
            {
                var builder = new HttpRequestBuilder()
                              .AddMethod(HttpMethod.Post)
                              .AddRequestUri(requestUri)
                              .AddContent(streamContent)
                              .AddBearerToken(bearerToken);

                return(await builder.SendAsync(client, cancellationToken));
            }
        }
示例#2
0
        public static async Task <HttpResponseMessage> GetWithQueryString(this HttpClient client, string requestUri, object value, string bearerToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            var builder = new HttpRequestBuilder()
                          .AddMethod(HttpMethod.Get)
                          .AddRequestUri(QueryHelpers.AddQueryString(requestUri, QueryStringHelper.ToKeyValue(value) ?? new Dictionary <string, string>()))
                          .AddBearerToken(bearerToken);

            return(await builder.SendAsync(client, cancellationToken));
        }
示例#3
0
        public static async Task <HttpResponseMessage> Delete(this HttpClient client, string requestUri, object value, JsonSerializerSettings serializerSettings, CancellationToken cancellationToken = default(CancellationToken))
        {
            var builder = new HttpRequestBuilder()
                          .AddMethod(HttpMethod.Delete)
                          .AddRequestUri(requestUri)
                          .AddContent(new JsonContent(value, serializerSettings));

            return(await builder.SendAsync(client, cancellationToken));
        }
示例#4
0
        public static async Task <HttpResponseMessage> Get(this HttpClient client, string requestUri, string bearerToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            var builder = new HttpRequestBuilder()
                          .AddMethod(HttpMethod.Get)
                          .AddRequestUri(requestUri)
                          .AddBearerToken(bearerToken);

            return(await builder.SendAsync(client, cancellationToken));
        }
        public static async Task <HttpResponseMessage> Patch(this HttpClient client, string requestUri, object value, JsonSerializerSettings serializerSettings, string bearerToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            var builder = new HttpRequestBuilder()
                          .AddMethod(new HttpMethod("PATCH"))
                          .AddRequestUri(requestUri)
                          .AddContent(new PatchContent(value, serializerSettings))
                          .AddBearerToken(bearerToken);

            return(await builder.SendAsync(client, cancellationToken));
        }
示例#6
0
        public static async Task <HttpResponseMessage> PostForm(this HttpClient client,
                                                                string requestUri, object value, string bearerToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            var builder = new HttpRequestBuilder()
                          .AddMethod(HttpMethod.Post)
                          .AddRequestUri(requestUri)
                          .AddContent(FormUrlEncodedContentExtensions.FromObject(value))
                          .AddBearerToken(bearerToken);

            return(await builder.SendAsync(client, cancellationToken));
        }
示例#7
0
        public static async Task <HttpResponseMessage> PostFile(this HttpClient client, string requestUri,
                                                                string filePath, string apiParamName, string bearerToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            var builder = new HttpRequestBuilder()
                          .AddMethod(HttpMethod.Post)
                          .AddRequestUri(requestUri)
                          .AddContent(new FileContent(filePath, apiParamName))
                          .AddBearerToken(bearerToken);

            return(await builder.SendAsync(client, cancellationToken));
        }
        public static async Task <HttpResponseMessage> Put(this HttpClient client,
                                                           string requestUri, object value, JsonSerializerOptions serializerOptions, string bearerToken, CancellationToken cancellationToken = default(CancellationToken), int timeoutSeconds = 0)
        {
            var builder = new HttpRequestBuilder()
                          .AddMethod(HttpMethod.Put)
                          .AddRequestUri(requestUri)
                          .AddContent(new JsonContent(value, serializerOptions))
                          .AddBearerToken(bearerToken);

            return(await builder.SendAsync(client, cancellationToken));
        }
        public static async Task <byte[]> GetBytesWithQueryString(this HttpClient client, string requestUri, int length, object value, string bearerToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            var builder = new HttpRequestBuilder()
                          .AddMethod(HttpMethod.Get)
                          .AddBytes(length)
                          .AddRequestUri(QueryHelpers.AddQueryString(requestUri, QueryStringHelper.ToKeyValue(value) ?? new Dictionary <string, string>()))
                          .AddBearerToken(bearerToken);

            var response = await builder.SendAsync(client, cancellationToken, HttpCompletionOption.ResponseHeadersRead);

            using (var stream = await response.Content.ReadAsStreamAsync())
            {
                var bytes     = new byte[length];
                var bytesread = stream.Read(bytes, 0, bytes.Length);
                stream.Close();
                return(bytes);
            }
        }
        public static async Task <HttpResponseMessage> PostAsStream(this HttpClient client,
                                                                    string requestUri, object value, JsonSerializerOptions serializerSettings, string bearerToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            var ms = new MemoryStream();

            await JsonSerializer.SerializeAsync(ms, value, serializerSettings);

            await ms.FlushAsync();

            ms.Seek(0, SeekOrigin.Begin);

            using (var streamContent = new StreamContent(ms))
            {
                var builder = new HttpRequestBuilder()
                              .AddMethod(HttpMethod.Post)
                              .AddRequestUri(requestUri)
                              .AddContent(streamContent)
                              .AddBearerToken(bearerToken);

                return(await builder.SendAsync(client, cancellationToken));
            }
        }