HTTPS message - what am I doing wrong?

I am making requests to webaddress to receive XML files over an HTTPS connection. But this relationship works like 50%. In most cases, this fails. Common error: "Socket error # 10060". Or "SSL connection error. EOF has been observed that violates the protocol." What am I doing wrong?

function SendRequest(parameters: string): IXMLDocument; var sPostData: TStringList; sHttpSocket: TIdHTTP; sshSocketHandler: TIdSSLIOHandlerSocketOpenSSL; resStream: TStringStream; xDoc: IXMLDocument; begin sPostData := TStringList.Create; try sPostData.Add('add some parameter to post' + '&'); sPostData.Add('add some parameter to post' + '&'); sPostData.Add('add some parameter to post' + '&'); sPostData.Add(parameters); sHttpSocket := TIdHTTP.Create; sshSocketHandler := TIdSSLIOHandlerSocketOpenSSL.Create; sHttpSocket.IOHandler := sshSocketHandler; sHttpSocket.Request.ContentType := 'application/x-www-form-urlencoded'; sHttpSocket.Request.Method := 'POST'; resStream := TStringStream.Create; sHttpSocket.Post(Self.sUrl, sPostData, resStream); xDoc := CreateXMLDoc; xDoc.LoadFromStream(resStream); Result := xDoc; resStream.Free; sHttpSocket.Free; sshSocketHandler.Free; sPostData.Free; except on E: Exception do begin TCommon.ErrorLog('errorLog.txt', DateTimeToStr(Now) + ' ' + E.Message); end end; end; 

Maybe I can do it differently, which works 100% when an internet connection is available?

Regards, evilone

+4
source share
1 answer

The "EOF" error assumes that you are connecting to a server that is not actually using SSL, or that SSL data may be corrupted.

Also, why do you include an explicit '&' character between your message data parameters? Do not do this, Indy will simply encode them and send his own '&' characters. Also, consider using TMemoryStream instead of TStringStream to provide IXMLDocumect.LoadFromStream () loads the original XML source data of the server as it is, rather than the modified version that RTL / VCL creates due to Unicode processing (TStringStream supports TEncoding).

Edit:. The URL you provided, an example of verifyUser () will look like this:

 const ERPLYAccountCode = '...'; function verifyUser(const user, pass: string; const sessionLength: Integer = 3600): IXMLDocument; var sPostData: TStringList; sHttpSocket: TIdHTTP; sshSocketHandler: TIdSSLIOHandlerSocketOpenSSL; resStream: TMemoryStream; xDoc: IXMLDocument; begin Result := nil; try resStream := TMemoryStream.Create; try sPostData := TStringList.Create; try sPostData.Add('clientCode=' + ERPLYAccountCode); sPostData.Add('request=verifyUser'); sPostData.Add('version=1.0'); sPostData.Add('responseType=XML'); sPostData.Add('responseMode=normal'); sPostData.Add('username=' + user); sPostData.Add('password=' + pass); sPostData.Add('sessionLength=' + IntToStr(sessionLength)); sHttpSocket := TIdHTTP.Create; try sshSocketHandler := TIdSSLIOHandlerSocketOpenSSL.Create(sHttpSocket); sHttpSocket.IOHandler := sshSocketHandler; sHttpSocket.Request.ContentType := 'application/x-www-form-urlencoded'; sHttpSocket.Post('https://www.erply.net/api/', sPostData, resStream); finally sHttpSocket.Free; end; finally sPostData.Free; end; resStream.Position := 0; xDoc := CreateXMLDoc; xDoc.LoadFromStream(resStream); Result := xDoc; finally resStream.Free; end; except on E: Exception do begin TCommon.ErrorLog('errorLog.txt', DateTimeToStr(Now) + ' ' + E.Message); end; end; end; 
+2
source

Source: https://habr.com/ru/post/1333137/


All Articles