Jump to content
mazluta

Error sending data: (12029) A connection with the server could not be established.

Recommended Posts

i have rest server witch listen to port 8090.

it suppose to get JSON with some params.

 

i have web APP that run in some domain - www.hanibaal-test.co.il

the server is in localhost :8090

image.thumb.png.814f33e0da53ba7411835d17023633e2.png

 

on the client side i do :

 

procedure TSignedDocumentsFrm.DoSignDocument_CA(PdfFileName : String);
var
  HttpClient : TNetHTTPClient;
  Response   : IHTTPResponse;
  JsonData   : TStringStream;
  AppUrl     : String;
  ResultPdfFileName : String;
  SignServerAddress : String;
  PdfBase64         : String;
  JpgBase64         : String;
  EncrtpPassWord    : String;
begin
  PdfBase64  := dm_Image.ImgToBase64(PdfFileName);
  JpgBase64  := dm_Image.ImgToBase64(UniMainModule.UserDataRec.User_Logo_FileName);
  EncrtpPassWord := EncryptStringCK(UniMainModule.UserDataRec.User_TokenPassword, AppEncryPass);
  JustWriteToLog('EncrtpPassWord = ' + EncrtpPassWord);
  SignServerAddress := UniMainModule.UserDataRec.User_CA_Sign_HttpAddrs +
                       ':' + IntToStr(UniMainModule.UserDataRec.User_CA_Sign_Port) + '/sign';
  JustWriteToLog('SignServerAddress : ' + SignServerAddress);

  Try
    JsonData := TStringStream.Create('{"PdfBase64Type": "base64", ' +
                                     ' "PdfBase64": "' + PdfBase64 + '", ' +
                                     ' "JpgBase64Type": "base64",' +
                                     ' "JpgBase64": "' + JpgBase64 + '", ' +
                                     ' "LogoTop": ' + IntToStr(UniMainModule.UserDataRec.User_Logo_Top) + ',' +
                                     ' "LogoLeft": ' + IntToStr(UniMainModule.UserDataRec.User_Logo_Left) + ',' +
                                     ' "LogoWidth": ' + IntToStr(UniMainModule.UserDataRec.User_Logo_Width) + ',' +
                                     ' "LogoHeight": ' + IntToStr(UniMainModule.UserDataRec.User_Logo_Height) + ',' +
                                     ' "CheckTokenStr": "' + UniMainModule.UserDataRec.User_Token_SerialNO + '",' +
                                     ' "CardSerialNO": "' + UniMainModule.UserDataRec.User_Token_SerialNO + '",' +
                                     ' "EncrptPassword": "' + EncrtpPassWord + '",' +
                                     ' "ReasonToSign": "' + UniMainModule.UserDataRec.User_SignReason + '",' +
                                     ' "ImageOpacity": ' + IntToStr(UniMainModule.UserDataRec.User_ImageOpacity) + '}', TEncoding.UTF8);

    AppUrl     := UniApplication.UniSession.URL;
    if AppUrl[Length(AppUrl)] = '/' then
    begin
      AppUrl[Length(AppUrl)] := ' ';
      AppUrl := Trim(AppUrl);
    end;

    HttpClient := TNetHTTPClient.Create(nil);
    //HttpClient.CustomHeaders['Origin'] := AppUrl;;
    HttpClient.CustomHeaders['Content-Type'] := 'application/x-www-form-urlencoded';
    HttpClient.PreemptiveAuthentication := true;
    JustWriteToLog('After Set HttpClient Origin to : ' + AppUrl);

    // Set request content type to JSON
    HttpClient.ContentType := 'application/json';
    //HttpClient.Accept      := 'application/json';

    // Send POST request with JSON data
    JustWriteToLog('Before Post Data to Server');
    JustWriteToLog('JsonData = ' + JsonData.ToString);

    Response := HttpClient.Post(SignServerAddress, JsonData);

    // Check response
    if Response.StatusCode = 200 then
    begin
      ResultPdfFileName :=  RemoveBackSlashChar(UniServerModule.LocalCachePath) +
                              '\MyResult.Pdf';
      Base64ToPDF(Response.ContentAsString,ResultPdfFileName);
      //JustWriteToLog(Response.ContentAsString);
      //ShowMessage('Response: ' + Response.ContentAsString);
    end
    else
    begin
      ShowMessage('Error: ' + Response.StatusText);
    end;
  finally
    JsonData.Free;
    HttpClient.Free;
  end;
end;


on the server side i have :

 

procedure TMyWebModule.WebModuleBeforeDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  JustWriteToLog('start dispath');
  JustWriteToLog('Request.Method = ' + Request.Method);

  Response.CustomHeaders.Values['Access-Control-Allow-Origin']      := '*';
  Response.CustomHeaders.Values['Access-Control-Allow-Headers']     := 'Origin, X-Requested-With, Content-Type, Accept';
  Response.CustomHeaders.Values['Access-Control-Allow-Methods']     := 'GET, POST, PUT, DELETE, OPTIONS';
  Response.CustomHeaders.Values['Access-Control-Allow-Credentials'] := 'true';
  Response.CustomHeaders.Values['Access-Control-Allow-Private-Network'] := '*';
  Response.CustomHeaders.Values['Access-Control-Expose-Headers']        := '';
  Response.CustomHeaders.Values['Access-Control-Max-Age']               := '86400';
  JustWriteToLog('On WebModuleBeforeDispatch - After Set Response.SetCustomHeader Access-Control-Allow-Origin' );

  if Trim(Request.GetFieldByName('Access-Control-Request-Headers')) <> '' then
  begin
    JustWriteToLog('On WebModuleBeforeDispatch - Trim(Request.GetFieldByName(Access-Control-Request-Headers))');
    Response.CustomHeaders.Values['Access-Control-Allow-Headers'] := Request.GetFieldByName('Access-Control-Request-Headers');
    Handled := True;
  end;

  if SameText(Request.Method, 'OPTIONS') then
  begin
    JustWriteToLog('On WebModuleBeforeDispatch - if SameText(Request.Method, Option)');
    Response.StatusCode := 204; // No Content
    Handled := True;
  end;
end;

 

something is blocking the request. and i don't no what....

i close firewall + AV

i don't see where and who block my call

i don't get any message in the logs (not in the client and not in the server), except "...connection can not be...."

i don't see any error in the console.log.

 

When I run both app in my localhost - all work fine and i get CA digital Sign PDF File

 

Share this post


Link to post
On 11/2/2024 at 1:07 PM, mazluta said:

i close firewall + AV

I'm not sure what you mean by this but I'd first check the Windows Firewall on the server to make sure it has port 8090 open.

 

This is almost always the case for me when I'm testing client/server app and it works find locally but not to a remote server.

Share this post


Link to post

Hi.  thanks for your answer.

i close the Firewall.

i close the AV.

i add inbound roll to open port TCP 8090 

for CORS it should "Ask" the server in the local for permition and i respond "OK" for CORS, but it did not rich to this "question".

 

i even don't know what is blocking?

where is it blocking?

who is blocking>

Share this post


Link to post

"Access-Control-Allow-Origin: *" and Credentials / "Access-Control-Allow-Credentials: true" do not work together.

 

Try with "Access-Control-Allow-Origin: null" or better a propper address.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×