【问题标题】:Postman gets data from api but .net core 2.2 app notPostman 从 api 获取数据,但 .net core 2.2 应用程序没有
【发布时间】:2019-06-09 17:06:08
【问题描述】:

我无法从 .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# 中,我有以下代码:

string response = await "http://192.168.43.96:9890/api/login".PostJsonAsync(new { login = "admin", password = "admin" }).ReceiveJson();

我使用流利的 HTTP 在 C# 中执行请求。我已经测试了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"
    }
}

最后我想在这个 .NET CORE 2.2 APP 中实现它。

请帮忙。

【问题讨论】:

  • 您能否提供有关您在 C# 中的请求的更多详细信息?
  • 我刚刚在 C# 中添加了有关请求的信息 - 请检查,如果您需要更多详细信息 - 请告诉我
  • 我看不到你如何使用令牌来验证你的请求
  • 你在代理后面吗?
  • @H.Herzl 现在不是关于验证令牌 - 我需要处理这个错误并在 C# 中获得完整的 HTTP 响应。之后 - 我将创建令牌验证。

标签: c http asp.net-core .net-core


【解决方案1】:

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

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; }
}

并使用 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)
{

}

【讨论】:

    猜你喜欢
    • 2021-10-18
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2021-01-20
    • 2020-01-12
    • 1970-01-01
    相关资源
    最近更新 更多