1

I have a software which is working faultlessly for 2 years now. This software connects to our remote server every 5 days to check for version updates as well as users license status.

But from yesterday it has suddenly started giving an error when connecting with our remote server. Again the confusing thing is that the error keeps on changing. Most of the time the error is:

Connection Closed Gracefully.

But sometimes another error is also reported, which is:

Socket Error # 10054

Connection reset by peer.

Here is the code that I am using:

Http := TIdHTTP.Create(nil);  //Create HTTP Object
Http.Request.UserAgent := 'Mozilla/5.0 (Windows NT x.y; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0';
Http.ReadTimeout := -1;
Http.HandleRedirects := True;

Response := TStringStream.Create();
Params := TStringStream.Create('', TEncoding.UTF8, False);
Params.WriteString(Format('data=%s', [TNetEncoding.URL.Encode(JsonData)]));
Http.Request.ContentType := 'application/x-www-form-urlencoded';

try
  Http.Post(Url, Params, Response);  //the moment this line is executed it triggers Error exception
except on E: Exception do
  ShowMessage(E.Message);
end;

Any ideas as to how to solve this problem? I am using Indy 10.6.2.5366 and Delphi Rio 10.3.

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
Yogi Yang 007
  • 5,147
  • 10
  • 56
  • 77
  • 1
    This is most likely a network error, nothing to do with your delphi code. Try other ways of connecting. Can you connect through a browser? CURL? has anything changed on the server? – Toby Allen Dec 31 '21 at 07:24
  • Regarding changes on server. Probably nothing has changes. My browser is able to connect without any problems and I am also able to open & operate the CPanel. – Yogi Yang 007 Dec 31 '21 at 07:30
  • 1
    When I open CPanle in browser I am getting a message: The firewall on this server is blocking your connection. You need to contact the server owner or hosting provider for further information. Your blocked IP address is: xxx.xxx.xxx.xxx The hostname of this server is: rs2xx.nsresponse.com – Yogi Yang 007 Dec 31 '21 at 07:37
  • 1
    When something works "faultlessly" for years, and then suddenly stops working without any code changes, it means something likely changed on the network/server. – Remy Lebeau Dec 31 '21 at 08:01
  • 1
    On a side note, the *preferred* way to send an `application/x-www-form-urlencoded` request with `TIdHTTP` is to use the overloaded version of `Post()` that takes a `TStrings` as input, not a `TStream`. The `TStrings` version of `Post()` will handle URL-encoding the data properly for you, when the `hoForceEncodeParams` flag is enabled in the `TIdHTTP.HTTPOptions` property (which it is by default). – Remy Lebeau Dec 31 '21 at 08:02
  • 1
    I'd ban you for `Gecko/20100101 Firefox/10.0`, because no human is able to achieve that old version anymore. And software which clearly isn't a web browser should identify as such - see [Is a "Hard Coded" user agent enough for a program to work on multiple computers?](https://stackoverflow.com/a/40103319/4299358) – AmigoJack Dec 31 '21 at 09:36
  • @AmigoJack, What should I replace this with? Please suggest. – Yogi Yang 007 Dec 31 '21 at 11:10
  • @RemyLebeau, Thanks you for your tip. Next time I will keep that is mind. But currently as the software is working properly I would not like to change it. – Yogi Yang 007 Dec 31 '21 at 11:11
  • @YogiYang007 Same as in the linked answer: `MyProgram/1.0 (+http://myprogram.org/what_i_am_doing.html)` – AmigoJack Dec 31 '21 at 11:36
  • @AmigoJack, So this means I will have to first create a file called - ```what_i_am_doing.html``` and then use this string. I will test this out once my current problem is solved. What have we to put in this html file? – Yogi Yang 007 Dec 31 '21 at 12:19
  • 1
    That's a pattern, not an actual filename - the URI is important, especially for humans being able to read any contact info. If you take that literally you better [read in an encyclopedia the article about user agents](https://en.wikipedia.org/wiki/User_agent). – AmigoJack Dec 31 '21 at 13:52
  • I propose you use TServerXMLHTTP instead of TIdHTTP. normally indy libraries has bug. – Amin Alinezhad Jan 01 '22 at 09:04
  • @AminAlinezhad,But I am sending data to server using Post and not in XML. – Yogi Yang 007 Jan 01 '22 at 12:56
  • @YogiYang007 There is no need to format your data to XML and you can send your data by POST or GET. ServerXMLHTTP is for MicroSoft and implemented for delphi in MSXML_TLB2 unit. you can learn mush more about this [here](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms766431(v=vs.85)) – Amin Alinezhad Jan 02 '22 at 07:54
  • @YogiYang007 what does the server's response actually look like? The errors you have shown would suggest that `TIdHTTP` is trying to read from the connection past the end of the response after the connection has been closed by the server, which *might* imply the response is malformed in a way that prevents `TIdHTTP` from detecting the end of the response correctly. – Remy Lebeau Jan 04 '22 at 21:30
  • @RemyLebeau, The problem has been finally solved. It was hosting company's configuration update problem which lead to this error. But now it is solved as the Hosting company updated their configurations. – Yogi Yang 007 Jan 26 '22 at 11:36

0 Answers0