Post Idhttp com Delphi

Asked

Viewed 9,927 times

3

I can do an HTML post quietly, but when I try to do it with Delphi, it presents me with this mistake:

http/1.1 406 not acceptable

Follow the code below:

function TForm1.UploadArquivo(server, script, caminhoarq : string) : boolean;
var
  Response : String;
  HTTPClient : TIdHTTP;
  Lista : TStringList;
begin
  result := False;
  HTTPClient := TidHTTP.Create;
  HTTPClient.ProtocolVersion := pv1_0;
  HTTPClient.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
  HTTPClient.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
  Response := '';
  Lista := TStringList.Create;
  try
    try
      Lista.Add('Arquivo='+ caminhoarq);

      Response := Trim(HTTPClient.Post(server + script, Lista));
      Label1.Caption := Response;
      if Response = 'OK' then
        Result := true;
    finally
      Lista.Free;
      HTTPClient.free;
    end;
  except
    on e:exception do  ShowMessage('Erro ao enviar arquivo ao servidor! Detalhes: '+e.Message);
  end;
end;

Use Delphi XE7, there is some particularity?

What am I doing wrong?

1 answer

3


This error may be because of the symbols, you should treat them before making the request!

HTTPClient := TidHTTP.Create;
HTTPClient.Request.ContentType := 'utf-8'; 

You can try that too:

Response := UTF8Decode(Trim(HTTPClient.Post(server + script, Lista)));

One more thing here:

HTTPClient.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';

As incredible as it sounds, the "space" there between compatible and Indy Library can cause the same problem, so switch to:

HTTPClient.Request.UserAgent := 'Mozilla/3.0 (compatible;Indy Library)';
  • THANK YOU!! All right friend.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.