ConnectionError between Python and ASP .Net core

I want to use the Python lib requests to connect to my ASP Net Core API point.

After trying to send a Json from Python to ASP Net core I get the following message:

ConnectionError: HTTPConnectionPool(host=’localhost’, port=5000): Max retries exceeded with url: /api/Search (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x7f66d3773880>: Failed to establish a new connection: [Errno 111] Connection refused’))

btw: I have only tried once to establish the connection 😉

import requests
import json

class SearchApi:

@staticmethod
 def findFile(request):
    
    headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
    url     = "http://localhost:5000/api/Search"
    response = requests.post(url, data=json.dumps(request.__dict__), headers=headers)

    return response.json()

and request class:

class Request:

def __init__(self, driveId, itemId, searchName):
    self.driveId = driveId
    self.itemId = itemId
    self.searchName = searchName

and Asp .Net Core back-End method:

        [ApiController]
        [Route("api/[controller]")]
      public class SearchController : ControllerBase
         [HttpPost]
       public async Task<IActionResult> SearchFile([FromBody] SearchInfos searchInfos)
       {
        //......
        Console.WriteLine("Ping ! ! ");
        return Ok();
    }

With the backend, unfortunately, no request arrives from Python, but the Postman or Swagger tests are successful.

What mistake have I made in the code ?

Answer

Update

The reason for the problem is that the ngrok proxy is used. Use the following command to solve this problem.

ngrok http -host-header=localhost:5000

Try to set keep_alive = False.

s = requests.session()
s.keep_alive = False

Try to add Connection=close in headers.

headers = {
            'Connection': 'close',
            'Content-type': 'application/json', 
            'Accept': 'application/json'
          }