I was making an application to send backup files to the Drive, however, when I try to send the files in chunks, I am getting a 400 error in the first request.
procedure TMainForm.btnTesteClick(Sender: TObject);
const
cURL = 'URL from requset';
cFilePath = 'path of File';
var
zIdHTTP: TIdHTTP;
zParams: TIdMultiPartFormDataStream;
zIOHandler: TIdSSLIOHandlerSocketOpenSSL;
zMemFile: TFileStream;
begin
zIdHTTP:= TIdHTTP.Create();
zParams:= TIdMultiPartFormDataStream.Create();
zIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create();
try
zIOHandler.SSLOptions.SSLVersions := [sslvTLSv1_2];
zIOHandler.SSLOptions.Mode := sslmUnassigned;
zIOHandler.SSLOptions.VerifyMode:= [];
zIOHandler.SSLOptions.VerifyDepth:=0;
zIdHTTP.IOHandler:= zIOHandler;
zIdHTTP.HandleRedirects:= True;
try
zMemFile:= TFileStream.Create(cFilePath, fmOpenReadWrite);
zIdHTTP.Request.CustomHeaders.AddValue('Content-Length', '8388608');
zIdHTTP.Request.CustomHeaders.AddValue('Content-Range', 'bytes 0-8388607/178498359');
zParams.AddFormField('data', 'application/octet-stream', '', zMemFile);
ShowMessage(zIdHTTP.Put(cURL, zParams));
except
on E: EIdHTTPProtocolException do ShowMessage(IntToStr(zIdHTTP.ResponseCode));
on E:Exception do ShowMessage('Error: Code: '+IntToStr(zIdHTTP.ResponseCode)+' - Message: '+E.Message);
end;
finally
FreeAndNil(zMemFile);
FreeAndNil(zIdHTTP);
end;
end;
the error message that appears is as follows: "Connection reset by peer"
The request is being made with the TIDHTTP component, but I also tested it with other components and the error continues to happen.
Does anyone know how to fix this error ?
I've tried it with other components like REST, and I've also tested it with insomnia.
Related
I'm trying to download a csv from the .gov.uk website, however, when I run the procedure an error is displayed: Socket Error 11001 Host not found.
procedure TFrm_Settings.btn_FetchPricesClick(Sender: TObject);
var
Url, LocalFile: String;
FileStrm: TFileStream;
Http : TIdHTTP;
begin
Url := 'http://www.gov.uk/government/uploads/system/uploads/attachment_data/file/400489/weekly_fuel_prices_csv.csv';
LocalFile := 'FuelPrices.csv';
FileStrm := TFileStream.Create(LocalFile, fmCreate);
Http:= TIDHTTP.Create(nil);
try
try
Http.Get(Url, FileStrm);
finally
FileStrm.Free;
end;
except
DeleteFile(LocalFile);
raise;
end;
I'm trying to download a file with the idHTTP component but its not working.
I included both OpenSSL DDLs (libeay32 & ssleay32.dll) to my project folder but its not working.
I'm using the idHTTP component with the TidSSLIOHandlerSocketOpenSSL IOHandler and SSL version SSLv3.
With this code I get an EIdHTTPProtocolException with the message "HTTP/1.1 403 Forbidden".
Why do I get this error and how can I get the download to work?
procedure TForm_Main.Button1Click(Sender: TObject);
var
fs: TFileStream;
begin
inherited;
fs := TFileStream.Create('E:\abc.html', fmCreate or fmShareDenyWrite);
try
LoadOpenSSLLibrary;
IdHTTP1.Get('https://www.testcloud.de/index.html', fs);
finally
fs.Free;
end;
end;
I am trying to log into craigslist using Delphi, and retrieve my account page (in order to gather a listing of all my posts)
However, I can't seem to get the login to work, what Am I doing wrong?
function TfrmMain.Login: string;
var
IdHTTP: TIdHTTP;
Request: TStringList;
Response: TMemoryStream;
begin
Result := '';
try
Response := TMemoryStream.Create;
try
Request := TStringList.Create;
try
Request.Add('op=login');
Request.Add('redirect=http://newyork.craigslist.org/');
Request.Add('login=' + myEmail);
Request.Add('password=' + myPassword);
IdHTTP := TIdHTTP.Create;
try
IdHTTP.AllowCookies := True;
IdHTTP.HandleRedirects := True;
IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
IdHTTP.Post('https://accounts.craigslist.org/login', Request, Response);
Result := IdHTTP.Get('https://accounts.craigslist.org/');
finally
IdHTTP.Free;
end;
finally
Request.Free;
end;
finally
Response.Free;
end;
except
on E: Exception do
ShowMessage(E.Message);
end;
end;
I get a exception class EIdIOHandlerPropInvalid with message 'IOHandler value is not valid' on the line:
IdHTTP.Post('https://accounts.craigslist.org/login', Request, Response);
thanks
See (Indy) TIdHTTP EIdSocketError Socket Error # 0 exceptions when downloading files and the suggestions in the comments. It looks like you should upgrade to a more recent version of Indy.
I know there's alot of Indy threads but I can't get one to match my case.
I have been given a URL with a username and password form. this then actions to a URL/reports.php on which there are multiple hyperlinks.
Each of these links will direct to a page with URL variables e.g. reports.php?report=variablename where a download will immediately start.
My thinking so far:
procedure TForm1.PostData(Sender: TObject);
var
paramList:TStringList;
url,text:string;
// IdHTTP1: TIdHTTP;
IdSSLIOHandlerSocket1: TIdSSLIOHandlerSocket;
idLogFile1 : TidLogFile;
begin
idLogFile1 := TidLogFile.Create(nil);
with idLogFile1 do
begin
idLogFile1.Filename := 'C:\HTTPSlogfile.txt';
idLogFile1.active := True;
end;
IdHTTP1 := TIdHTTP.Create(nil);
IdSSLIOHandlerSocket1 := TIdSSLIOHandlerSocket.Create(nil);
IdSSLIOHandlerSocket1.SSLOptions.Method := sslvSSLv23;
IdHTTP1.IOHandler := IdSSLIOHandlerSocket1;
IdHTTP1.HandleRedirects := true;
IdHTTP1.ReadTimeout := 5000;
IdHTTP1.Intercept := idLogFile1;
paramList:=TStringList.create;
paramList.Clear;
paramList.Add('loguser=testuser');
paramList.Add('logpass=duke7aunt');
paramList.Add('logclub=8005');
url := 'https://www.dfcdata.co.uk/integration/reports.php?report=live';
try
IdHTTP1.Post(url,paramList);
except
on E:Exception do
begin
showMessage('failed to post to: '+url);
ShowMessage('Exception message = '+E.Message);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
reportType : String;
begin
PostData(Self);
reportType := 'live';
GetUrlToFile('',reportType+'.csv');
end;
procedure TForm1.GetUrlToFile(AURL, AFile : String);
var
Output : TMemoryStream;
success : Boolean;
begin
success := True;
Output := TMemoryStream.Create;
try
try
IdHTTP1.Get(AURL, Output);
IdHTTP1.Disconnect;
except
on E : Exception do
begin
ShowMessage('Get failed to GET from '+IdHTTP1.GetNamePath +'. Exception message = '+E.Message);
success := False;
end;
end;
if success = True then
begin
showMessage('Filed saved');
Output.SaveToFile(AFile);
end;
finally
Output.Free;
end;
end;
On each try I get "IOHandler is not valid" error. Obviously I'm not posting correctly to the initial page but can anyone advise me on what I'm missing? Also can I simply then hit the download URL after login or will I have to use cookies?
Thanks
There are several bugs in your code:
1) PostData() is requesting an HTTPS URL, but it is not assigning an SSL-enabled IOHandler to the TIdHTTP.IOHandler property. You need to do so.
2) Button1Click() is passing a URL to GetUrlToFile() that does not specify any protocol, so TIdHTTP will end up treating that URL as relative to its existing URL, and thus try to download from https://www.testurl.com/test/testurl.com/test/reports.phpinstead of https://testurl.com/test/reports.php. If you want to request a relative URL, don't include the hostname (or even the path in this case, since you are sending multiple requests to the same path, just different documents).
3) you are leaking the TIdHTTP object.
Issue 1) has now been resolved in another post:
Delphi 5 Indy/ics SSL workaround?
However I would greatly appreciate help on the rest, as follows.
Would I need to make a GET call with the same IdHTTP object and additional URL variable? or should I create a new IdHTTP object?
Would I need to record the session using cookies or can all of this be done with the same call?
Is the GET call above actually what I need to save a csv to file? I may also choose to handle it directly as the data will need importing anyway.
Currently the code gets the error: EIdHTTPProtocolException
I'm upgrading from Delphi 2005 to Delphi 2010.
I'm having this problem : the following procedure works well on D2005 but on D2010 I got always the result :
<HTML><HEAD><TITLE>401 Unauthorized</TITLE></HEAD>
<BODY><H1>401 Unauthorized</H1>
Your client does not have permission to get URL /axis-cgi/date.cgi from this server.
</BODY></HTML>
On the procedure, I try to connect twice because on D2005, sometimes I got an Unauthorized answer at my first try, and then I can connect at the second time with no problem. With D2010 I always get the Unauthorized answer.
The Url = 'http://user:pass#xxx.xxx.xxx.xxx/axis-cgi/date.cgi?action=get'
function TViewCameraForm.HttpGet(idHTTP : TidHTTP; Url : AnsiString): AnsiString;
Var
Res : AnsiString;
Begin
idHTTP1.Disconnect;
try
Res := idHTTP1.Get(Url);
If Pos('Unauthorized', Res) > 0 Then
Res := idHTTP1.Get(Url);
Result := Res;
except
on E: EIdHTTPProtocolException do begin
Result := E.ErrorMessage
end;
on E: Exception do begin
Result := E.message;
end;
end;
End;
Thanks
Sam
Did you try to authenticate using Basic Auth?
...
idHTTP1.Request.BasicAuthentication := True;
idHTTP1.Request.Username := 'user';
idHTTP1.Request.Password := 'pass';
Res := idHTTP1.Get(Url);
(using user:pass#website does not conform to the HTTP specification btw)