【问题标题】:Exception -"Cannot send a content-body with this verb-type" using httpclient.sendasync异常 - “无法发送具有此动词类型的内容主体”使用 httpclient.sendasync
【发布时间】:2018-09-27 07:51:03
【问题描述】:

已经尝试在 SO 中搜索类似问题,但这些问题无助于解决。

场景:

拥有一个以 Web API 应用程序作为后端的 Web 客户端应用程序。如果来自此客户端 - Web API,则调用相同的 URI 时没有问题,但是当我尝试从另一个 WebAPI 服务应用程序触发 API 请求时会发生异常(例如,它是用于访问作为服务运行的报告的单独项目使用 Telerik 报告)。

代码:

 private async Task<HttpStatusCode> AccessService()
        {
            HttpResponseMessage response = null;
            try
            {

                using (HttpRequestMessage request = new HttpRequestMessage()
                {
                    Content = new System.Net.Http.StringContent(JSONValues, System.Text.Encoding.UTF8, "application/json"),
                    RequestUri = new Uri(UriString),
                    Method = CallMethod
                })
                {

                   request.Headers.Authorization =  new AuthenticationHeaderValue("Bearer", Utils.Token) ;

                    response = await httpClient.SendAsync(request);

                    if (response.IsSuccessStatusCode)
                    {
                        string httpResponse = await response.Content.ReadAsStringAsync();

                            httpString = JsonConvert.DeserializeObject<string[]>(httpResponse);
                    }
                    ErrorCode = response.StatusCode;
                    response.Dispose();
                }
            }
            catch (Exception ex)
            {
                    Misc.Error= ex.Message;
            }
            finally
            {
                response.Dispose();
            }

            return ErrorCode;

        }

例外:

无法发送具有此动词类型的内容主体。

  at System.Net.HttpWebRequest.CheckProtocol(Boolean onRequestStream)
   at System.Net.HttpWebRequest.BeginGetRequestStream(AsyncCallback callback, Object state)
   at System.Net.Http.HttpClientHandler.StartGettingRequestStream(RequestState state)
   at System.Net.Http.HttpClientHandler.PrepareAndStartContentUpload(RequestState state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

当我从客户端应用程序 (asp.net mvc) 访问它时,这工作正常,当我从 Report Services API 应用程序调用它时会出现问题。是不是跟这个有关系。

编辑:

Web 客户端项目是 asp.net mvc core 2.1,所以我认为它在使用 HttpClient.SendAsync 的 GET 方法中的内容时可以工作,但发生此问题的 Reports Services 应用程序是目标 .net 461 。

我强烈怀疑这可能是原因,因为 .net 框架不允许 GET 操作的内容?有什么解决办法吗? Link

TIA

【问题讨论】:

  • 失败时CallMethod的值是多少?
  • @ToddMenier CallMethod 是 GET

标签: asp.net-web-api asp.net-core-mvc dotnet-httpclient


【解决方案1】:

根据提供的描述,如果您使用 get 方法,您通常在 get 请求中没有正文。更多详情请参考以下解决方案:

http://stackoverflow.com/questions/3981564/cannot-send-a-content-body-with-this-verb-type

【讨论】:

  • 谢谢,但已经检查过了……我已经有一个请求正文发送
猜你喜欢
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多