Postman从api获取数据,但.NETCore2.2应用程序没有

Postman从api获取数据,但.NETCore2.2应用程序没有,c,http,asp.net-core,.net-core,C,Http,Asp.net Core,.net Core,我无法在.NET CORE 2.2应用程序中从后端API检索响应。但是邮递员 我可以检索数据。在.NET CORE 2.2应用程序中引发错误: An unhandled exception occurred while processing the request. IOException: The server returned an invalid or unrecognized response. System.Net.Http.HttpConnection.FillAsync() Ht

我无法在.NET CORE 2.2应用程序中从后端API检索响应。但是邮递员

我可以检索数据。在.NET CORE 2.2应用程序中引发错误:

An unhandled exception occurred while processing the request.
IOException: The server returned an invalid or unrecognized response.
System.Net.Http.HttpConnection.FillAsync()

HttpRequestException: Error while copying content to a stream.
System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)

HttpRequestException: Error while copying content to a stream.
System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task<HttpResponseMessage> sendTask, HttpRequestMessage request, CancellationTokenSource cts, bool disposeCts)
Test.Controllers.HomeController.Login(AuthUser authUser) in HomeController.cs
+
            var response = await client.PostAsync("http://192.168.43.96:9890/api/login", content);
Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
我用C#和fluent HTTP完成请求。我已经测试了httpClient,但没有成功

后端是用C编写的。 生成后端的响应如下所示:

HTTP/1.1 200 OK
Content-Length: 262
Connection: keep-alive
Content-Type: application/json
Server: Simple HTTP with C server
Date: Sun Jun  9 19:03:06 2019


{
    "data": [{
            "id":   "1",
            "login":    "admin",
            "first_name":   "Main",
            "last_name":    "Admin",
            "email":    "main.admin@myhelpdesk.com",
            "role_id":  "1"
        }],
    "token":    {
        "value":    "c509fe9566db8302ef11d78974579bc9a825d617c44d78bfeda959d3a8d9f163"
    }
}

最后,我想让它在这个.NETCore2.2应用程序中实现


请提供帮助。

您可以尝试在客户端创建实体,如:

public class Datum
{
    public string id { get; set; }
    public string login { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string email { get; set; }
    public string role_id { get; set; }
}

public class Token
{
    public string value { get; set; }
}

public class RootObject
{
    public List<Datum> data { get; set; }
    public Token token { get; set; }
}
公共类数据
{
公共字符串id{get;set;}
公共字符串登录{get;set;}
公共字符串first_name{get;set;}
公共字符串last_name{get;set;}
公共字符串电子邮件{get;set;}
公共字符串角色_id{get;set;}
}
公共类令牌
{
公共字符串值{get;set;}
}
公共类根对象
{
公共列表数据{get;set;}
公共令牌{get;set;}
}
并使用HttpClient将请求发送到服务器端,读取响应并反序列化对象:

try
{
    using (HttpClient client = new HttpClient())
    {
        var content = new StringContent(jsonInString, Encoding.UTF8, "application/json");
        var response = await client.PostAsync(url, content);
        if (response != null)
        {
            var jsonString = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<RootObject>(jsonString);
        }
    }
}
catch (Exception ex)
{

}
试试看
{
使用(HttpClient=new HttpClient())
{
var content=newstringcontent(jsonInString,Encoding.UTF8,“application/json”);
var response=wait client.PostAsync(url、内容);
if(响应!=null)
{
var jsonString=await response.Content.ReadAsStringAsync();
返回JsonConvert.DeserializeObject(jsonString);
}
}
}
捕获(例外情况除外)
{
}

您能用C#提供更多关于您的请求的详细信息吗?我刚刚在C#中添加了关于请求的信息-请检查,如果您需要更多详细信息-请告诉我我我不知道您如何使用令牌验证您的请求您是否支持代理?@H.Herzl现在不是关于令牌验证的问题-我需要处理此错误并获取完整信息C#中的HTTP响应。之后-我将创建令牌验证。
try
{
    using (HttpClient client = new HttpClient())
    {
        var content = new StringContent(jsonInString, Encoding.UTF8, "application/json");
        var response = await client.PostAsync(url, content);
        if (response != null)
        {
            var jsonString = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<RootObject>(jsonString);
        }
    }
}
catch (Exception ex)
{

}