Luz
Luz

Reputation: 1

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:\Users\sserg\source\repos\Cocal\Cocal\Src\Xml\TransformarEmPDF\ConversorPDF.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:\Users\sserg\source\repos\Cocal\Cocal\Src\Xml\TransformarEmPDF\ConversorPDF.cs:linha 123
       in System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
       in Cocal.Src.Xml.TransformarEmPDF.ConversorPDF.<SendRequest>d__19.MoveNext() na C:\Users\sserg\source\repos\Cocal\Coc

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:\xampp\apache\logs\error.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.

Upvotes: 0

Views: 67

Answers (0)

Related Questions