1

I'm stucked with one problem here.

I need to upload data from Delphi App (or service) to web site using PHP scripts. I tried the IdHTTP.Post method but always receive HTTP/1.1 406 Not Acceptable response. I really don't know if is a server-side error or client-side error.

The Delphi code I'm trying is:

procedure TMain_UploadTest.btTryPostClick(Sender: TObject);
var
  PostData: TIdMultipartFormDataStream;
begin
  PostData := TIdMultipartFormDataStream.Create;
  try
    PostData.AddFile('file', 'simple.xml', 'application/xml');
    idhttp1.Post('http://' + edServer.Text + '/quick_import.php', PostData)
  finally
    PostData.Free;
  end;
end;

and PHP script are this:

<?php
//quick_import.php
sleep(3);
$output = '';

if(isset($_FILES['file']['name']) &&  $_FILES['file']['name'] != '')
{
 $valid_extension = array('xml');
 $file_data = explode('.', $_FILES['file']['name']);
 $file_extension = end($file_data);
 if(in_array($file_extension, $valid_extension))
 {

  $file_name = "uploads/xmlfile" . date("YmdHis") . ".xml";

  $inp = fopen($_FILES['file']['tmp_name'], "r");
  $outp = fopen($file_name, "w");

  while (!feof($inp)) {
    $buffer = fread($inp, 8192);
    fwrite($outp, $buffer);
  }

  fclose($inp);
  fclose($outp);
 }
}
?>

but always receive HTTP/1.1 406 Not Acceptable response when at online servers.

My localhost version works perfectly!

My last chance are uploading files via FTP and call PHP scripts after that, but I'm not happy with this solution... already tried Host Support Chat many times, but without success!

Anyone can help me how to workaround this HTTP 406 error?

Any method are accepted!

Delphi client and PHP server are the only pre-requisite of this job.

Thanks in advance!


I've already tried all of these line code, alone, combined etc... no success!

IdHTTP1.Request.CustomHeaders.Clear;
IdHTTP1.Request.Clear;
//IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
IdHTTP1.Request.ContentEncoding := 'multipart/form-data';



idhttp1.Request.AcceptEncoding := '*';

IdHTTP1.Request.Accept := 'text/html';
IdHTTP1.Request.ContentType := 'multipart/form-data';
//IdHTTP1.Request.AcceptCharset := 'UTF-8';
IdHTTP1.Request.AcceptEncoding:= '*';//'*';

idhttp1.Request.AcceptCharset := 'iso-8859-1';
IdHTTP1.Request.UserAgent := 'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0';

between

    PostData.AddFile('file', 'simple.xml', 'application/xml');

and

    idhttp1.Post('http://' + edServer.Text + '/quick_import.php', PostData)

the IdHTTP1.Request.RawHeaders.Text of working localhost server are:

Connection: keep-alive

Content-Type: multipart/form-data; boundary=--------100617210443541

Content-Length: 1273

Host: 192.168.15.11

Accept: text/html, /

User-Agent: Mozilla/3.0 (compatible; Indy Library)

the IdHTTP1.Response.RawHeaders.Text of working localhost server are:

Date: Sat, 07 Oct 2017 00:04:40 GMT

Server: Apache/2.2.29 (Unix) DAV/2 PHP/5.4.45 mod_ssl/2.2.29

OpenSSL/0.9.8zg

X-Powered-By: PHP/5.4.45

Content-Length: 582

Keep-Alive: timeout=5, max=100

Connection: Keep-Alive

Content-Type: text/html

and finally the IdHTTP1.Response.RawHeaders.Text of online server are:

Server: nginx/1.12.1

Date: Sat, 07 Oct 2017 00:07:32 GMT

Content-Type: text/html; charset=iso-8859-1

Content-Length: 226

Connection: keep-alive

the IdHTTP1.Request.RawHeaders.Text of online server with all modifiers code above comented are:

Connection: keep-alive

Content-Type: multipart/form-data; boundary=--------100617211049477

Content-Length: 1273

Host: www.hostgatorserver.com.br

Accept: text/html, /

User-Agent: Mozilla/3.0 (compatible; Indy Library)

when use:

IdHTTP1.Request.CustomHeaders.Clear;
IdHTTP1.Request.Clear;
idhttp1.Request.AcceptEncoding := '*';
IdHTTP1.Request.Accept := 'text/html';
IdHTTP1.Request.ContentType := 'multipart/form-data';
IdHTTP1.Request.AcceptEncoding:= '*';//'*';

the IdHTTP1.Request.RawHeaders.Text of online server are:

Connection: keep-alive

Content-Type: multipart/form-data; boundary=--------100617211635196

Content-Length: 1273

Host: online.hostgatorserver.com.br

Accept: text/html

Accept-Encoding: *

User-Agent: Mozilla/3.0 (compatible; Indy Library)

tried

idhttp1.Request.AcceptEncoding := '*';

but without success!

alijunior
  • 317
  • 1
  • 12
  • I'm at Hostgator server too... – alijunior Oct 06 '17 at 22:54
  • Nice, so the answer there has a decent chance of working for you (eg explicitly setting `AcceptEncoding`) – ccKep Oct 06 '17 at 23:29
  • The problem in the asked question indicated as duplicate are in Android App. The author said then Windows version are working... I tried all suggestions of answers but without success... can help me? – alijunior Oct 07 '17 at 01:05

0 Answers0