示例#1
0
        private void SaveReport(SqlConnection connection, HkpvReport report, HkpvReportInfo info, int institutionId)
        {
            using (var ms = report.WriteToStream(asJson: false, compressed: true))
            {
                var userId = this.GetRowId("Users", _authContext.Principal.Identity.Name, connection);

                SqlCommand insert = new SqlCommand("insert into Messages([UserId], [InstitutionId], [Hash_SHA256], [Month], [Year], [Date], [Data]) values(@userId, @institutionId, @hash, @month, @year, @date, @data)", connection);
                insert.Parameters.AddWithValue("@userId", userId);
                insert.Parameters.AddWithValue("@institutionId", institutionId);
                insert.Parameters.AddWithValue("@hash", info.HashSHA256);
                insert.Parameters.AddWithValue("@month", info.Month);
                insert.Parameters.AddWithValue("@year", info.Year);
                insert.Parameters.AddWithValue("@date", DateTime.Now);
                insert.Parameters.AddWithValue("@data", ms.ToArray());
                try
                {
                    insert.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, ex.Message);
                    throw ex;
                }

                _logger.LogInformation("Report saved");
            }
        }
示例#2
0
        public async Task <SendResult> Send(HkpvReport report, string username, string password)
        {
            using (var client = new HttpClient())
                using (var data = report.WriteToStream())
                {
                    var url     = new Uri(_address, $"{report.FromD.Year}/{report.FromD.Month}");
                    var content = new ByteArrayContent(data.ToArray());

                    if (!string.IsNullOrEmpty(username))
                    {
                        var byteArray = Encoding.ASCII.GetBytes($"{username}:{password}");
                        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                    }

                    var response = await client.PutAsync(url, content);

                    if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        return new SendResult()
                               {
                                   IsValid = false, ErrorMessage = "Unauthorized"
                               }
                    }
                    ;

                    SendResult result = null;

                    var responseMsg = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(responseMsg))
                    {
                        try
                        {
                            result = JsonConvert.DeserializeObject <SendResult>(responseMsg);
                        }
                        catch (Exception e)
                        {
                            result = new SendResult()
                            {
                                IsValid = false, ErrorMessage = e.Message
                            };
                        }
                    }
                    else
                    {
                        result = new SendResult()
                        {
                            IsValid = false, ErrorMessage = $"{response.StatusCode}: Empty"
                        };
                    }

                    return(result);
                }
        }
    }