gpt4 book ai didi

c# - PostAsync 返回异常

转载 作者:行者123 更新时间:2023-12-04 09:14:17 27 4
gpt4 key购买 nike

我正在使用控制台应用程序进行容量测试,在这些测试中,我调用了我在 API 中创建的 JobTitles 路由,在本例中是处理该路由的 Controller 。

对于我正在使用的 Post 函数,我使用了 HttpClient PostAsJsonAsync() 方法。因为我已经在其他代码中使用过这个函数,所以我没有遇到太多问题,但现在它返回异常 System.Net.Http.HttpRequestException: An error occurred while sending the request.

我没有使用任何 .Result 调用,因为它最终可能会导致死锁;我也尝试使用 .ConfigureAwait (false) 但它没有用。我还尝试使用 Easy.Common RestClient()但同样的错误发生了,我已经没有关于错误可能是什么的选项了。抱歉,我无法仅通过查看堆栈跟踪来弄清楚错误是什么,我仍然不知道如何弄清楚。

我把代码放在这里是为了更好看,因为它有点大:

https://repl.it/repls/JubilantMoccasinArrays

我试图创建一个通用方法,因为在其他代码中我为每个实体创建了一个 Post,利用这个问题,我想知道除了 RestClient 和 HttpClient 之外是否还有另一个方法可以用来发送多个调用而不会由于许多 API 条目而导致发货被取消,因为它会自动取消以避免 DDoS。

以及我从 RestClient 获得的堆栈跟踪:

Unhandled exception. System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.IO.IOException: The response ended prematurely.
at System.Net.Http.HttpConnection.FillAsync()
at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at CodenApp.Studio2Me.VolumeTest.UsefulFunctions.HTTPFunctions`1.Post(String route, T content) in C:\Users\Yasmin Kruger\Documents\Studio2Me\Studio2Me\Tests\CodenApp.Studio2Me.VolumeTest\UsefulFunctions\HTTPFunctions.cs:line 29
at CodenApp.Studio2Me.VolumeTest.Classes.JobTitleData.JobTitleDataGenerator(Int32 x) in C:\Users\Yasmin Kruger\Documents\Studio2Me\Studio2Me\Tests\CodenApp.Studio2Me.VolumeTest\Classes\JobTitleData.cs:line 21
at CodenApp.Studio2Me.VolumeTest.Program.Main(String[] args) in C:\Users\Yasmin Kruger\Documents\Studio2Me\Studio2Me\Tests\CodenApp.Studio2Me.VolumeTest\Program.cs:line 22
at CodenApp.Studio2Me.VolumeTest.Program.<Main>(String[] args)

编辑1:

我只是发送一个请求只是为了测试它是否有效,我需要发送至少 x * 6 个 JobTitles 而不计算有更多的类一起发送,所以我需要一种可以发送大量的方法对 API 的请求数。

EDIT2:

来自 HttpClient 的堆栈跟踪:

    System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.IO.IOException: The response ended prematurely.
at System.Net.Http.HttpConnection.FillAsync()
at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at CodenApp.Studio2Me.VolumeTest.UsefulFunctions.HTTPFunctions`1.PostTest(String route, T content) in C:\Users\Yasmin Kruger\Documents\Studio2Me\Studio2Me\Tests\CodenApp.Studio2Me.VolumeTest\UsefulFunctions\HTTPFunctions.cs:line 51

编辑 3:

主要.cs:

using System;
using CodenApp.Studio2Me.VolumeTest.Classes;
using CodenApp.Studio2Me.VolumeTest.UsefulFunctions;

namespace CodenApp.Studio2Me.VolumeTest
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
// await JobTitleData.JobTitleDataGenerator(1);
await JobTitleData.JobTitleDataGenerator2(1);
}
}
}

HTTPFunction.cs:

using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Easy.Common.Extensions;
using Easy.Common.Interfaces;
using Newtonsoft.Json;
using Shouldly;
using RestClient = Easy.Common.RestClient;

namespace CodenApp.Studio2Me.VolumeTest.UsefulFunctions
{
public class HTTPFunctions<T> where T : class
{
const string APIAddress = "http://localhost:5001/";

public static async Task Post(string route, T content)
{
var endpointUri = new Uri(APIAddress + route);
ServicePointManager.FindServicePoint(endpointUri).ConnectionLeaseTimeout.ShouldBe(-1);

var jsonContent = JsonConvert.SerializeObject(content);
var contentString = new StringContent(jsonContent, Encoding.UTF8, "application/json");
contentString.Headers.ContentType = new MediaTypeHeaderValue("application/json");

using IRestClient client = new RestClient();
HttpResponseMessage response = await client.PostAsync(endpointUri, contentString); //This won't work too
//client.Timeout.ShouldBe(15.Seconds()); Sometimes cause an TimeOut Exception
Console.WriteLine(response);
}

public static async Task PostTest(string route, T content)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(APIAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

try
{
HttpResponseMessage response = await client.PostAsJsonAsync(route, content).ConfigureAwait(false); //still not working
if(!response.IsSuccessStatusCode)
{
Console.WriteLine(content);
}
}
catch(Exception e)
{
//Using this just to get exception
Console.WriteLine(e.Message);
}
}
}
}
}

JobTitleData.cs:

using System;
using System.Threading.Tasks;
using Bogus;
using CodenApp.Studio2Me.Core.Entities.StoreEntities;
using CodenApp.Studio2Me.VolumeTest.UsefulFunctions;

namespace CodenApp.Studio2Me.VolumeTest.Classes
{
public class JobTitleData
{
public static async Task JobTitleDataGenerator(int x)
{
var faker = new Faker("pt_BR");

for(int i = 0; i < x; i++)
{
JobTitle jobTitle = new JobTitle();
jobTitle.Name = faker.Name.JobTitle();
jobTitle.StoreId = 1;

await HTTPFunctions<JobTitle>.Post("v1/Store/JobTitles", jobTitle);
}
}

public static async Task JobTitleDataGenerator2(int x)
{
var faker = new Faker("pt_BR");

for(int i = 0; i < x; i++)
{
JobTitle jobTitle = new JobTitle();
jobTitle.Name = faker.Name.JobTitle();
jobTitle.StoreId = 1;

await HTTPFunctions<JobTitle>.PostTest("v1/Store/JobTitles", jobTitle);
}
}
}
}

与@Andy 讨论了对代码的几处更改,同样对我返回了相同的错误,我正在其他机器上进行测试并尝试通过 Insomnia 调用相同的路由来检查它是否不是没有尚未配置。现在它认为这是服务器阻止,因为我什至无法返回对象响应。

提议的改变:

using (var msg = new HttpRequestMessage(HttpMethod.Post, new UriBuilder(APIAddress) { Path = route }.Uri))
{
msg.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");
using (var resp = await _client.SendAsync(msg))
{
resp.EnsureSuccessStatusCode();
await resp.Content.ReadAsStringAsync();
}
}
}
}

编辑4:

经过多次测试并使用不同程序调用路由后,我发现这确实是服务器问题,更确切地说是 HTTP 认证问题,因为它是阻止调用的问题。我将对项目进行更改以获取更多信息,并在 edit5 中返回决定的内容并发布答案以完成此问题。

最佳答案

所以我相信你的问题出在这条线上:

HttpResponseMessage response = await client.PostAsJsonAsync(route, 
content).ConfigureAwait(false); //still not working
if(!response.IsSuccessStatusCode)
{
Console.WriteLine(content);
}

您还没有阅读任何内容。你刚刚得到了响应对象。您必须对该响应执行此操作:

var objectReturned = await response.Content.ReadAsAsync<WhateverObject>();

或者,测试:

var stringDataReturned = await response.Content.ReadAsStringAsync();

一个典型的场景是这样的:

var response = await client.PostAsJsonAsync(route, content);
repsonse.EnsureSuccessStatusCode();

var data = await response.ReadAsStringAsync();
Console.WriteLine(data);

关于c# - PostAsync 返回异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63289326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com