RestSharp响应内容始终为空,但仍使用Execute记录日志

时间:2019-05-31 15:54:15

标签: c# restsharp

发送基本GET时,响应。返回时内容始终为NULL。奇怪的是它将内容记录在标准执行中,而不是异步记录中。我机智的尽头。任何帮助将不胜感激。

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

常规方法(带有标识信息X'd):

                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;

很显然,我希望它既记录日志又返回实际内容。好的,上师,我在做什么错了?

1 个答案:

答案 0 :(得分:0)

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