1

I'm doing a HTTPS post with this code to azurewebsites. http://MYAPP.azurewebsites.net/api/MYFUNC

I'm currently using this code:

procedure TForm1.OriginalTest();
var
  lHTTP: TIdHTTP;
  HTTPResult: string;
  RequestBody: TStream;
  URL: String;
  Body: string;
  IOHandler: TIdSSLIOHandlerSocketOpenSSL;

begin
  lHTTP := TIdHTTP.Create;
  try
    Body := '{}';
    RequestBody := TStringStream.Create(Body, TEncoding.UTF8);
    lHTTP.Request.Accept := '';
    lHTTP.Request.UserAgent := '';

    lHTTP.Request.CustomHeaders.Add('x-functions-key:<your api key>');
    lHTTP.ConnectTimeout := 24000;
    lHTTP.ReadTimeout := 24000;
    IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    IOHandler.SSLOptions.Method := sslvTLSv1_2;

    lHTTP.IOHandler := IOHandler;
    try
      URL := 'https://<yourapp>.azurewebsites.net/api/<funcname>';

      HTTPResult := lHTTP.Post(url, RequestBody);

      Memo1.Lines.Add(HTTPResult);
    except
      on E:Exception do
      begin
        Memo1.Lines.Add(Format('Error sending data. Error: %s', [E.Message] ));
      end;
    end;
  finally
    lHTTP.Free;
    RequestBody.Free;
  end;
end;

For whatever reason, this code gives me the following error:

Error sending data. Error: Socket Error # 10054 Connection reset by peer.

I tried making a simple HTTPS Post using .NET with HttpWebRequest , and it works fine. What am I doing wrong here?

Yako
  • 191
  • 1
  • 2
  • 12
  • Did you try some other HTTP client, like [curl](https://stackoverflow.com/questions/7172784)? – mjn Jul 06 '18 at 18:30
  • 2
    The error means the server is abortively closing the connection on its end. It probably doesn't like something in your SSL/TLS handshake, or your HTTP request. Are you strictly required to use TLS 1.2 only? Have you tried enabling TLS 1.0 and TLS 1.1? And why are you clearing the `UserAgent`? And why are you not specifying a `ContentType`? Also, you are leaking the `IOHandler` object, as you don't call `Free()` on it, or assigning an `Owner` to it. – Remy Lebeau Jul 06 '18 at 22:39
  • @mjn Yes, like I wrote I tried writing a simple app in .NET and C# and it works fine there. I also downloaded another HTTP client - works there as well. This problem only occurs with my Delphi code. I've tried all different TLS versions and none seems to work. I've tried setting the contentype and useragent. The reason I left them empty is because it works when I leave them empty in my C# code. I also did change the code so I assign an owner to the IOHandler and free the object, doesn't make any difference. That was just some test code, I was trying all kinds of different code. – Yako Jul 08 '18 at 19:32
  • 1
    @Yako then you need to sniff the actual HTTPS traffic and see what is different between your app that doesn't work and other apps that do work. – Remy Lebeau Jul 09 '18 at 01:45
  • @RemyLebeau Really appreciate your help, thank you. I tried filtering the port 443 in Wireshark. It looks like my Delphi code first sends a package (protocol="TCP") and it gets a response immediately (red color in Wireshark) with the info "Connection refused." When I run my .NET C# code , it sends a package (protocol = TLSv1) and gets a response.. I've taken a screenshot.. Let me know if you need to see anything else, I'm no networking expert. [Screenshot](https://i.imgur.com/6tSONOs.png) – Yako Jul 09 '18 at 12:20
  • @RemyLebeau I also get a "Server Hello" with the C# code, so it works fine. – Yako Jul 09 '18 at 12:24
  • @RemyLebeau ... and of course the .NET code establishes a connection first before it gets the "Server Hello". – Yako Jul 09 '18 at 12:36
  • 1
    @Yako "Connection Refused" happens at the TCP layer, not the HTTP layer. It means either 1) the client attempted to connect to an IP:Port that is not listening for connections, 2) the IP:Port is listening but has too many pending connections, or 3) the IP:Port is being blocked by firewall/router, antivirus, etc. Most likely, your app is not connecting to the same IP:Port that the other apps are connecting to. Use Wireshark to verify that. – Remy Lebeau Jul 09 '18 at 16:26

1 Answers1

0

I just gave up and used WinApi.WinInet instead.

Yako
  • 191
  • 1
  • 2
  • 12