已经尝试在SO中搜索类似问题,但这些问题无助于解决。
具有一个Web客户端应用程序,该应用程序具有一个Web API应用程序作为后端。如果是从此客户端-Web API调用相同的URI,则没有问题,但是当我尝试从另一个WebAPI Services应用程序触发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核心2.1,因此我认为它在使用HttpClient.SendAsync的GET方法中的内容时有效,但是发生此问题的Reports Services应用程序是目标.net 461。
我非常怀疑这可能是原因,因为.net框架不允许GET操作使用内容?有什么解决方法吗? Link
TIA
答案 0 :(得分:-1)
根据提供的说明,如果使用get方法,则get请求中通常没有正文。有关更多详细信息,请参阅以下解决方案:
http://stackoverflow.com/questions/3981564/cannot-send-a-content-body-with-this-verb-type