Board index » delphi » Problems using TIdHttp 9 with Proxy

Problems using TIdHttp 9 with Proxy

Hi,

  anybody have one samples of the solution or explanation about the problema
bellow:

  I'm trying to make HTTP POST, with TidHttp Component. The problem is ISA
Proxy Server. Wich properties must be setting for success ?

  Here is my code, is returned the message "HTTP/1.1 502 Proxy Error ( Host
is unreachable. For more information about this event, see ISA Server
Help. )".

  function TForm1.bExecutarHTTPSQL2(psURL: string;
                                  psSQL: string;
                                  out psXmlRetorno: PChar;
                                  out psErro: string): boolean;
{
  Objetivo : Conectar no WebService ADPWeb, executar o comando SQL
             devolvendo o XML de retorno

Quote
}

var
  aStream: TMemoryStream;
  Params: TStringStream;
  bRetorno: boolean;
  oConexaoHTTP: TidHTTP;
  sDados, sSelect, stabela, sColuna, sColunas, sNome, sTotalRegistros:
string;
begin

  bRetorno := true;

  try

    sColunas := '';

    // Monta string que ser enviada ao WebServiceSQL do ADPWeb
    // a partir do SQL de entrada (parametro psSQL)
    sDados := '<?xml version%3D' + #39 + '1.0' + #39 + ' encoding%3D' + #39
+ 'ISO-8859-1' + #39 + '?>' +
              '<root><ticket>' + sTicket + '</ticket>';

    // Extrai do comando SQL apenas a clausula SELECT, para saber
    // todas as colunas necessrias no comando

    sSelect := copy(psSQL, 1, pos('from', LowerCase(psSQL)) - 1);
    sSelect := Trim(StringReplace(LowerCase(sSelect), 'select', '', []));

    sSelect := sSelect + ',';

    while (length(sSelect) > 0) do
    begin

      stabela := copy(sSelect, 1, pos(',', sSelect) - 1);
      sColuna := copy(stabela, pos('.', stabela) + 1, length(stabela));
      stabela := copy(stabela, 1, pos('.', stabela) - 1);
      sNome := sColuna;

      // Verifica se n?o existe nenhum campo com mesmo nome
      // de retorno no XML (atributo "name" do elemento "var")
      while pos(' ' + sNome + ' ', sColunas) > 0 do
      begin

        if bENumero(copy(sNome, 1, length(sNome))) = true then
          sNome := sNome + IntToStr(StrToInt(copy(sNome, 1,
length(sColuna))) + 1)
        else
          sNome := sNome + '1';

      end;

      sDados := sDados + '<var name="' + sNome +
                '" field="' + stabela + '.' + sColuna + '"/>';

      sColunas := sColunas + ' ' + sNome + ' ';

      sSelect := trim(copy(sSelect, pos(',', sSelect) + 1,
length(sSelect)));

    end;

    sDados := sDados + '<select>' + StringReplace(psSQL, '%', '%25',
[rfReplaceAll]) + '</select></root>';
    sDados := StringReplace(sDados, '=', '%3D', [rfReplaceAll]);

    sDados := 'field_ticket=' + sTicket +
              '&field_rotina=sql-task' +
              '&field_xml=' + sDados;

    psErro := '';

    // Inicializa??o de variveis e objetos
    oConexaoHTTP := TidHTTP.Create(nil);
    aStream := TMemoryStream.create;
    Params := TStringStream.create('');
    oConexaoHTTP.Request.ContentType := 'application/x-www-form-urlencoded';

    with oConexaoHTTP do
    begin
      try
        ProxyParams.BasicAuthentication := true;
        ProxyParams.ProxyServer := txtProxyServer.text;
        ProxyParams.ProxyPort := StrToInt(txtProxyPort.text);
        ProxyParams.ProxyUsername := txtUserProxy.text;
        ProxyParams.ProxyPassword := txtPasswordProxy.text;

        Post(psURL, Params, aStream);

      except
        on e: exception do
        begin
          psErro := e.Message;
          bRetorno := false;
        end;
      end;
    end;

    aStream.WriteBuffer(#0' ', 1);

    psXmlRetorno := PChar(aStream.Memory);

  except
    on e: exception do
    begin
      psErro := e.Message;
      bRetorno := false;
    end;

  end;

  if oConexaoHTTP <> nil then
    FreeAndNil(oConexaoHTTP);

  Result := bRetorno;

end;

Very, very thanks ....

 

Re:Problems using TIdHttp 9 with Proxy


This may be a solution, or at least a workaround.
http://makeashorterlink.com/?T53F325A2

Quote
"Lincoln" <lamo...@adp.com.br> wrote in message

news:3dec9abb@newsgroups.borland.com...
Quote
> Hi,

>   anybody have one samples of the solution or explanation about the
problema
> bellow:

>   I'm trying to make HTTP POST, with TidHttp Component. The problem
is ISA
> Proxy Server. Wich properties must be setting for success ?

>   Here is my code, is returned the message "HTTP/1.1 502 Proxy Error
( Host
> is unreachable. For more information about this event, see ISA
Server
> Help. )".

>   function TForm1.bExecutarHTTPSQL2(psURL: string;
>                                   psSQL: string;
>                                   out psXmlRetorno: PChar;
>                                   out psErro: string): boolean;
> {
>   Objetivo : Conectar no WebService ADPWeb, executar o comando SQL
>              devolvendo o XML de retorno
> }
> var
>   aStream: TMemoryStream;
>   Params: TStringStream;
>   bRetorno: boolean;
>   oConexaoHTTP: TidHTTP;
>   sDados, sSelect, stabela, sColuna, sColunas, sNome,
sTotalRegistros:
> string;
> begin

>   bRetorno := true;

>   try

>     sColunas := '';

>     // Monta string que ser enviada ao WebServiceSQL do ADPWeb
>     // a partir do SQL de entrada (parametro psSQL)
>     sDados := '<?xml version%3D' + #39 + '1.0' + #39 + '
encoding%3D' + #39
> + 'ISO-8859-1' + #39 + '?>' +
>               '<root><ticket>' + sTicket + '</ticket>';

>     // Extrai do comando SQL apenas a clausula SELECT, para saber
>     // todas as colunas necessrias no comando

>     sSelect := copy(psSQL, 1, pos('from', LowerCase(psSQL)) - 1);
>     sSelect := Trim(StringReplace(LowerCase(sSelect), 'select', '',
[]));

>     sSelect := sSelect + ',';

>     while (length(sSelect) > 0) do
>     begin

>       stabela := copy(sSelect, 1, pos(',', sSelect) - 1);
>       sColuna := copy(stabela, pos('.', stabela) + 1,
length(stabela));
>       stabela := copy(stabela, 1, pos('.', stabela) - 1);
>       sNome := sColuna;

>       // Verifica se n?o existe nenhum campo com mesmo nome
>       // de retorno no XML (atributo "name" do elemento "var")
>       while pos(' ' + sNome + ' ', sColunas) > 0 do
>       begin

>         if bENumero(copy(sNome, 1, length(sNome))) = true then
>           sNome := sNome + IntToStr(StrToInt(copy(sNome, 1,
> length(sColuna))) + 1)
>         else
>           sNome := sNome + '1';

>       end;

>       sDados := sDados + '<var name="' + sNome +
>                 '" field="' + stabela + '.' + sColuna + '"/>';

>       sColunas := sColunas + ' ' + sNome + ' ';

>       sSelect := trim(copy(sSelect, pos(',', sSelect) + 1,
> length(sSelect)));

>     end;

>     sDados := sDados + '<select>' + StringReplace(psSQL, '%', '%25',
> [rfReplaceAll]) + '</select></root>';
>     sDados := StringReplace(sDados, '=', '%3D', [rfReplaceAll]);

>     sDados := 'field_ticket=' + sTicket +
>               '&field_rotina=sql-task' +
>               '&field_xml=' + sDados;

>     psErro := '';

>     // Inicializa??o de variveis e objetos
>     oConexaoHTTP := TidHTTP.Create(nil);
>     aStream := TMemoryStream.create;
>     Params := TStringStream.create('');
>     oConexaoHTTP.Request.ContentType :=

'application/x-www-form-urlencoded';

- Show quoted text -

Quote

>     with oConexaoHTTP do
>     begin
>       try
>         ProxyParams.BasicAuthentication := true;
>         ProxyParams.ProxyServer := txtProxyServer.text;
>         ProxyParams.ProxyPort := StrToInt(txtProxyPort.text);
>         ProxyParams.ProxyUsername := txtUserProxy.text;
>         ProxyParams.ProxyPassword := txtPasswordProxy.text;

>         Post(psURL, Params, aStream);

>       except
>         on e: exception do
>         begin
>           psErro := e.Message;
>           bRetorno := false;
>         end;
>       end;
>     end;

>     aStream.WriteBuffer(#0' ', 1);

>     psXmlRetorno := PChar(aStream.Memory);

>   except
>     on e: exception do
>     begin
>       psErro := e.Message;
>       bRetorno := false;
>     end;

>   end;

>   if oConexaoHTTP <> nil then
>     FreeAndNil(oConexaoHTTP);

>   Result := bRetorno;

> end;

> Very, very thanks ....

Other Threads