【问题标题】:RestSharp Response Content Always Null But Still Logs With ExecuteRestSharp 响应内容始终为空,但仍记录 Execute
【发布时间】:2019-05-31 15:54:14
【问题描述】:

发送基本 GET 时,response.Content 在返回时始终为 NULL。奇怪的是它会在标准执行上记录内容,而不是在异步上。我无计可施。任何帮助将不胜感激。

我尝试使用标准的 client.Execute 和 client.ExecuteAsync,除了日志记录部分之外,两者似乎都有相同的结果。

常规方法(带有识别信息的X'd out):

                RestClient myClient = new RestClient(uri);
                var request = new RestRequest(Method.GET);

                request.AddHeader("cache-control", "no-cache");
                request.AddHeader("Connection", "keep-alive");
                request.AddHeader("accept-encoding", "gzip, deflate");
                request.AddHeader("Host", "XXXXX");
                request.AddHeader("Cache-Control", "no-cache");
                request.AddHeader("Accept", "*/*");
                request.AddHeader("Authorization", "XXX");

                Serilog.Log.Information($"Request is {request}");

                IRestResponse response = myClient.Execute(request);
                var htmlString = response.Content;
                Serilog.Log.Information($"Request is {response.Content}");

                return htmlString;

executeAsync 方法:

                RestClient myClient = new RestClient(uri);
                var request = new RestRequest(Method.GET);

                request.AddHeader("cache-control", "no-cache");
                request.AddHeader("Connection", "keep-alive");
                request.AddHeader("accept-encoding", "gzip, deflate");
                request.AddHeader("Host", "XXXXX");
                request.AddHeader("Cache-Control", "no-cache");
                request.AddHeader("Accept", "*/*");
                request.AddHeader("Authorization", "XXXXXXXXXX");

                Serilog.Log.Information($"Request is {request}");
                var htmlString = "";
                myClient.ExecuteAsync(request, response =>
                {
                    htmlString = response.Content;
                    Serilog.Log.Information($"Response is {response.Content}");
                });
                return htmlString;

显然,我希望它既记录又返回实际内容。好的,大师们,我做错了什么?

【问题讨论】:

  • myClient.ExecuteAsync 阻止吗?否则,为什么要在之后设置htmlString?该请求很可能仍在“进行中”,或者...?
  • 我认为你需要一个await——看看这个帖子:stackoverflow.com/questions/21779206/…
  • 我也尝试了 await,与该线程相同,它仍然让我很适合。

标签: c# restsharp


【解决方案1】:

你一定是在开玩笑吧。它一直在工作(至少是常规执行),但是字符串开头的空格太多以至于 VS 2017 没有将其显示为本地。我必须深入研究才能看到它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 2020-07-23
    • 2016-03-27
    相关资源
    最近更新 更多