예제 #1
0
        public async Task <HttpResponseMessage> SendCommandAsync(HttpMethod method, Uri uri, object data, Func <StreamWriter, Task> onStreamReady, NetworkCredential credential)
        {
            var requestMessage = new HttpRequestMessage(method, uri);

            string encodedAuthorization = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(credential.UserName + ":" + credential.Password));

            requestMessage.Headers.Add("Authorization", "Basic " + encodedAuthorization);

            if (db.LoggerAvailable)
            {
                db.Log("==============================");
                db.Log(DateTime.Now.ToString());
                db.Log("sending http request:");
                db.Log($"url: {uri.ToString()}");
                db.Log($"method: {method.ToString()}");
                if (db.Setting.Logger.HttpHeaders)
                {
                    db.Log($"headers:");
                    foreach (var h in requestMessage.Headers)
                    {
                        db.Log($"{h.Key} : {string.Join(" ", h.Value)}");
                    }
                    foreach (var h in httpClient.DefaultRequestHeaders)
                    {
                        db.Log($"{h.Key} : {string.Join(" ", h.Value)}");
                    }
                }
                if (db.Setting.Logger.LogOnlyLightOperations == false)
                {
                    db.Log($"data: {new DocumentSerializer(db).SerializeWithoutReader(data)}");
                }
            }

            if (onStreamReady == null && data != null)
            {
                requestMessage.Content = new JsonContent(db, data);
            }
            if (onStreamReady != null)
            {
                requestMessage.Content = new GenericStreamContent(db, onStreamReady);
            }

            var responseMessage = await httpClient.SendAsync(requestMessage).ConfigureAwait(false);

            if (db.LoggerAvailable)
            {
                db.Log("==============================");
                db.Log(DateTime.Now.ToString());
                db.Log("received http response:");
                db.Log($"url: {uri.ToString()}");
                db.Log($"status-code: {responseMessage.StatusCode.ToString()}");
                if (db.Setting.Logger.HttpHeaders)
                {
                    db.Log($"headers:");
                    foreach (var h in responseMessage.Headers)
                    {
                        db.Log($"{h.Key} : {string.Join(" ", h.Value)}");
                    }
                }
                if (db.Setting.Logger.LogOnlyLightOperations == false)
                {
                    db.Log($"data: {new DocumentSerializer(db).SerializeWithoutReader(await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false))}");
                }
            }

            if (responseMessage.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new AuthenticationException($"The user '{credential.UserName}' is not authorized");
            }

            return(responseMessage);
        }