【问题标题】:How to make a Log of a Post in an web Api如何在 Web Api 中创建帖子日志
【发布时间】:2018-02-06 17:52:08
【问题描述】:

我想知道如何知道 Web Api 在我发送的 Post 中接收到的内容。

这是我正在使用的代码:

var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://d6dc30b8-0ee0-4-231-b9ee.azurewebsites.net/");
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Accept = "application/vnd.lyoness.servicesv1 + json";
                httpWebRequest.Headers.Add("Date" + tempo); 

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();

            }
            MessageBox.Show(json);

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var teste = streamReader.ReadToEnd();
                MessageBox.Show(teste);
            }

【问题讨论】:

  • 如果你只是为了开发需要这个,你可以在发出请求的机器上使用 Fiddler。它会捕获您发送到服务的内容以及您从中收到的内容。
  • 什么是提琴手?我该怎么做?
  • telerik.com/download/fiddler-wizard。 Fiddler 是一个实用程序,可以从您的 PC 中捕获所有 HTTP 流量。在其中,您将看到您的应用程序发出的请求
  • 没有它我可以使用任何代码或任何东西吗?
  • 如果您可以访问 Web 服务,您可以在该端进行一些日志记录。但是 fiddler 仍然是您轻松实现这一目标的最佳选择。 Fiddler 是免费的,是我日常使用的工具,我强烈推荐它。

标签: c# json post asp.net-web-api


【解决方案1】:

保持你当前的模型,如果你只是在做一个简单的记录,你可以做这样的事情

string url = "https://d6dc30b8-0ee0-4-231-b9ee.azurewebsites.net/";
string method = "POST";
string contentType = "application/json";
string accept = "application/vnd.lyoness.servicesv1 + json";
string headers = "Date";

string myLog = string.Format("URL = {0} | METHOD = {1} | CONTENT TYPE = {2} | ACCEPT = {3} | HEADERS = {4} + {5}", url, method, contentType, accept, headers, tempo);

var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = method;
httpWebRequest.ContentType = contentType;
httpWebRequest.Accept = accept;
httpWebRequest.Headers.Add(headers + tempo); 

然后您可以将myLog 写入文本文件或其他内容以进行记录。

【讨论】:

    猜你喜欢
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多