Hi,
I am currently writing an app to get data from one webform, doing something with data, and than submitting a form with file uploaded. All works till submitting a form. Server is refusing request continiously. I have checked with Fiddler what sort of data and headers are being sent by browser while submitting a form a tried to copy it, without luck. I hope you will be able to help me with this. Here is a code submitting a form (taken from one of the posts on this forums and modified)
function HttpPostFile(const URL, FileName: string;
const Data: TStream; const ResultData: TStrings): Boolean;
var
HTTP: THTTPSend;
Bound, s: string;
Header:TStringList;
const
CRLF = #13#10;
begin
Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary';
HTTP := THTTPSend.Create;
Header:=TStringList.Create;
try
s := '--' + Bound + CRLF;
s := s + 'Content-Disposition: form-data; name="file1"; filename="'+filename+ '"'+ CRLF;
s := s + ' Content-Type: application/octet-stream' + CRLF + CRLF;
s:=EncodeURLElement(s);
WriteStrToStream(HTTP.Document, s);
HTTP.Document.CopyFrom(Data, 0);
s := CRLF + '--' + Bound + '--' + CRLF;
s := s + 'Content-Disposition: form-data; name="B1"' + CRLF + CRLF +'Upload' + CRLF; //check this!
s := CRLF + '--' + Bound + '--' + CRLF;
s := s + 'Content-Disposition: form-data; name="file1db"' + CRLF + CRLF +file1db + CRLF;
s := CRLF + '--' + Bound + '--' + CRLF;
s:=EncodeURLElement(s);
WriteStrToStream(HTTP.Document, s);
Header.Add('HTTP/1.1 200 OK');
Header.Add('Server: nginx/1.4.6');
Header.Add('Content-Type: text/html; charset=UTF-8');
Header.Add('Connection: keep-alive');
Header.Add('X-Powered-By: PHP/5.3.28');
Header.Add('Connection: keep-alive');
Header.Add('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
Header.Add('Pragma: no-cache');
HTTP.MimeType := 'multipart/form-data; boundary=' + Bound;
HTTP.UserAgent:='Safari';
HTTP.Headers.AddStrings(Header);
Result := HTTP.HTTPMethod('POST', URL);
if Result then
ResultData.LoadFromStream(HTTP.Document);
finally
HTTP.Free;
end;
end;
This is an action calling the function:
try
readyfile:=TFileStream.Create(UpLink, fmOpenRead);
List:=TStringList.Create;
HttpPostFile(urlsave, UpLink, readyfile, List);
memo1.Lines:=List;
break;
finally
readyfile.Free;
end;
Where UpLink is a location of file to uploaded. It might have something to do with server accepted encoding. It's "Accept-Encoding: gzip, deflate", so browser is sending it as "Content-Encoding: gzip". Also, there is something to dowith cookies, Fiddler is showing:
Set-Cookie: frontend=62rgvscdej8a1pn796lk8vvid1; expires=Mon, 19-Jan-2015 07:48:30 GMT; path=/
Any suggestions, please?