Why Do I Get “400 Bad Request” After 100 Parallel Requests Using PHP and C# HttpClient?

I’m working on a project where I convert XML files into PDFs, sending HTTP POST requests from a C# application to a PHP server. At first everything works fine, but after processing about 100 simultaneous requests, the server starts responding with “400 Bad Request”.

I’ve already adjusted settings on the client and server, but the problem continues. I’m looking for guidance on what could be causing this and how to resolve it.

Client C#

private async Task SendRequest(string xmlKey, string diretorioPdf, string contentXml)
{
    client.DefaultRequestHeaders.Add("xmlKey", xmlKey);
    client.DefaultRequestHeaders.Add("diretorioPdf", diretorioPdf);

    var content = new StringContent($"{{"content":"{Convert.ToBase64String(Encoding.UTF8.GetBytes(contentXml))}"}}", Encoding.UTF8, "application/json");

    var response = await client.PostAsync("http://localhost:3000/conversao.php", content);
    response.EnsureSuccessStatusCode();
}

Server PHP

The server decodes the Base64 content and processes the XML. It validates the required headers and generates a PDF file.

$headers = getallheaders();
if (!isset($headers['xmlKey']) || !isset($headers['diretorioPdf'])) {
    header('Content-Type: application/json');
    echo json_encode(["status" => "Missing headers"]);
    exit();
}

$jsonData = file_get_contents('php://input');
$data = json_decode($jsonData, true);

if (!isset($data['content'])) {
    echo "Missing 'content' field.";
    exit();
}

$xml = base64_decode($data['content']);
// Processes the XML and generates the PDF...

What I Tried

Reduced the Competition Threshold in the C# Client

private readonly SemaphoreSlim semaphore = new SemaphoreSlim(10); // Maxde 10 threads

Error

After starting to run, the first 78 XML files are converted and then this error occurs:

   The response status code does not indicate success: 400 (Bad Request).
       in System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
       in Cocal.Src.Xml.TransformarEmPDF.ConversorPDF.<SendRequest>d__19.MoveNext() in C:UsersssergsourcereposCocalCocalSrcXmlTransformarEmPDFConversorPDF.cs:linha 155
    --- End stack trace of the previous location where the exception was thrown ---
       in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       in System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       in Cocal.Src.Xml.TransformarEmPDF.ConversorPDF.<ProcessRequest>d__18.MoveNext() in C:UsersssergsourcereposCocalCocalSrcXmlTransformarEmPDFConversorPDF.cs:linha 123
       in System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
       in Cocal.Src.Xml.TransformarEmPDF.ConversorPDF.<SendRequest>d__19.MoveNext() na C:UsersssergsourcereposCocalCoc

Validated the Data Before Sending: I added validations in the C# code to ensure that the headers and XML content were correct before sending.

Error in (C:xamppapachelogserror.log)

[Mon Dec 09 16:52:23.643126 2024] [ssl:warn] [pid 10384:tid 612] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Dec 09 16:52:23.769786 2024] [core:warn] [pid 10384:tid 612] AH00098: pid file C:/xampp/apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Mon Dec 09 16:52:23.773776 2024] [ssl:warn] [pid 10384:tid 612] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Dec 09 16:52:24.651449 2024] [mpm_winnt:notice] [pid 10384:tid 612] AH00455: Apache/2.4.58 (Win64) OpenSSL/3.1.3 PHP/8.2.12 configured -- resuming normal operations
[Mon Dec 09 16:52:24.651449 2024] [mpm_winnt:notice] [pid 10384:tid 612] AH00456: Apache Lounge VS17 Server built: Oct 18 2023 13:03:18
[Mon Dec 09 16:52:24.651449 2024] [core:notice] [pid 10384:tid 612] AH00094: Command line: 'c:\xampp\apache\bin\httpd.exe -d C:/xampp/apache'
[Mon Dec 09 16:52:24.657432 2024] [mpm_winnt:notice] [pid 10384:tid 612] AH00418: Parent: Created child process 33444
[Mon Dec 09 16:52:25.432362 2024] [ssl:warn] [pid 33444:tid 632] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Dec 09 16:52:25.534090 2024] [ssl:warn] [pid 33444:tid 632] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Dec 09 16:52:25.646789 2024] [mpm_winnt:notice] [pid 33444:tid 632] AH00354: Child: Starting 150 worker threads.
[Wed Dec 11 23:26:10.601035 2024] [ssl:warn] [pid 26360:tid 600] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Wed Dec 11 23:26:10.688648 2024] [core:warn] [pid 26360:tid 600] AH00098: pid file C:/xampp/apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Wed Dec 11 23:26:10.693636 2024] [ssl:warn] [pid 26360:tid 600] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Wed Dec 11 23:26:11.366501 2024] [mpm_winnt:notice] [pid 26360:tid 600] AH00455: Apache/2.4.58 (Win64) OpenSSL/3.1.3 PHP/8.2.12 configured -- resuming normal operations
[Wed Dec 11 23:26:11.366501 2024] [mpm_winnt:notice] [pid 26360:tid 600] AH00456: Apache Lounge VS17 Server built: Oct 18 2023 13:03:18
[Wed Dec 11 23:26:11.366501 2024] [core:notice] [pid 26360:tid 600] AH00094: Command line: 'c:\xampp\apache\bin\httpd.exe -d C:/xampp/apache'
[Wed Dec 11 23:26:11.371489 2024] [mpm_winnt:notice] [pid 26360:tid 600] AH00418: Parent: Created child process 26504
[Wed Dec 11 23:26:12.029726 2024] [ssl:warn] [pid 26504:tid 604] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Wed Dec 11 23:26:12.109550 2024] [ssl:warn] [pid 26504:tid 604] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Wed Dec 11 23:26:12.173341 2024] [mpm_winnt:notice] [pid 26504:tid 604] AH00354: Child: Starting 150 worker threads.

PHP version

PHP 8.2.12 (cli) (built: Oct 24 2023 21:15:15) (ZTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.2.12, Copyright (c) Zend Technologies

Even doing all this, the XMLs were converted from 78 files to 78 files and then the error occurred.

Can you help me understand what is happening? I couldn’t solve it using generative AIs.

New contributor

Luz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật