记录WebClient的请求,响应和异常

时间:2015-03-18 15:39:41

标签: c# .net web

我想记录(保存到数据库或文件等)所有请求和响应,但WebClient除外。 我使用派生类:public class GzipWebClient:WebClient {...}

我应该在哪里捕获包含数据的上传请求,下载响应和例外?或者我应该覆盖一些方法?

我可以使用“受保护的覆盖WebRequest GetWebRequest(Uri地址)”来捕获数据吗?

有些使用:

    private T GetDeserializedResponse<T>(string url)
    {
        using (var wc = new GzipWebClient())
        {
            wc.Encoding = Encoding.UTF8;
            string fullUrl = BaseUrl + url;
            string response = wc.DownloadString(fullUrl);

            try
            {
                return JsonConvert.DeserializeObject<T>(response);
            }
            catch
            {
                _logger.Error(response);
                throw;
            }
        }
    }

        string url = shop.Warehouse != null ?
            string.Format("/api/v1/cabinet/{0}/shop_create.json", MasterApiKey) :
            string.Format("/api/v1/{0}/shop_create.json", MasterApiKey);

        string name = shop.Name;
        string namePostfix = DateTime.Now.ToString("yyMMddhhmmss");
        if ((name + namePostfix).Length > 64)
            name = name.Substring(0, 64 - namePostfix.Length);

        string data = shop.Warehouse != null ?
            string.Format("name={0}&warehouse={1}&address={2}", name + namePostfix, shop.Warehouse.Id, shop.Address) :
            string.Format("name={0}&address={1}", name + namePostfix, shop.Address);

        using (var wc = new GzipWebClient())
        {
            wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            wc.Encoding = Encoding.UTF8;

            string fullUrl = BaseUrl + url;

            string response = wc.UploadString(fullUrl, data);

            var shopData = JsonConvert.DeserializeObject<DDeliveryCreateShopResponse>(response);

            if (!shopData.Success)
            {
                throw new DeliveryCreateStoreException(shop.Name);
            }

            if (string.IsNullOrEmpty(shopData.IdKeyPair.Key))
            {
                throw new DeliveryCreateStoreException(shop.Name);
            }

            shop.Id = shopData.IdKeyPair.Id;
            shop.Key = shopData.IdKeyPair.Key;
            return shop;
        }

2 个答案:

答案 0 :(得分:4)

如果您使用HttpClient来调用网络服务并使用.Net 4.5,则会更容易。

您可以像这样创建LoggingHandler

public class LoggingHandler : DelegatingHandler
{
    public LoggingHandler()
        : this(new HttpClientHandler())
    { }

    public LoggingHandler(HttpMessageHandler innerHandler)
        : base(innerHandler)
    { }

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var activityId = Guid.NewGuid();
        using (new Tracer("Service Call", activityId))
        {
            var entry = new LogEntry { Severity = TraceEventType.Verbose, Title = "Request" };
            if (Logger.ShouldLog(entry))
            {
                entry.Message = request.ToString();
                if (request.Content != null)
                {
                    entry.Message += Environment.NewLine;
                    entry.Message += await request.Content
                        .ReadAsStringAsync()
                        .ConfigureAwait(false);
                }
                Logger.Write(entry);
            }

            var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

            entry = new LogEntry { Severity = TraceEventType.Verbose, Title = "Response" };
            if (Logger.ShouldLog(entry))
            {
                entry.Message = response.ToString();
                if (response.Content != null)
                    entry.Message += await response.Content
                        .ReadAsStringAsync()
                        .ConfigureAwait(false);

                Logger.Write(entry);
            }

            return response;
        }
    }
}

然后创建HttpClient:

//hook all http request and response
var hc = new HttpClient(new LoggingHandler());

HttpClient课程的文档: https://msdn.microsoft.com/en-us/library/system.net.http.httpclient%28v=vs.118%29.aspx

比较HttpClientWebClient Need help deciding between HttpClient and WebClient

答案 1 :(得分:0)

您必须在应用程序的最低点,在DAL中记录异常(如果有),以便处理数据库的异常。