邮递员从api获取数据,但没有从.net core 2.2应用获取

时间:2019-06-09 17:06:08

标签: 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()

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中实现它。

请帮助。

1 个答案:

答案 0 :(得分:0)

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

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将请求发送到服务器端,读取响应并反序列化Object:

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)
{

}