Log in to website from Delphi - delphi

I would ask if someone was kind enough to explain to me how to login at webpage from Delphi app. All the examples I've found here have proved useless to me or I'm doing something wrong. I'm tired of the search and the code that does not work.
There is no error message, I even get page code into Memo but seems it's code from login page (not account [dashboard] page) - seems this code can't pass auth at all and I don't know why.
What is wrong in this code :
procedure Login;
var
HTTP: TIdHTTP;
Param: TStringList;
S: String;
begin
HTTP := TIdHTTP.Create(nil);
HTTP.CookieManager := Main_Form.CookieManager;
Param := TStringList.Create;
Param.Clear;
Param.Add('login=example');
Param.Add('password=example');
try
HTTP.Get ('http://www.filestrum.com/login.html');
HTTP.Post('http://www.filestrum.com/login.html', Param);
S := HTTP.Get ('http://www.filestrum.com/?op=my_account');
Main_Form.Memo2.Lines.Add(S);
finally
HTTP.Free;
Param.Free;
end;
end;
or with this version :
procedure Login;
var
HTTP: TIdHTTP;
S: String;
begin
HTTP := TIdHTTP.Create(nil);
HTTP.CookieManager := Main_Form.CookieManager;
HTTP.Request.BasicAuthentication := True;
HTTP.Request.Username := 'example';
HTTP.Request.Password := 'example';
HTTP.AllowCookies := True;
HTTP.HandleRedirects := True;
S := HTTP.Get ('http://www.filestrum.com/?op=my_account');
Main_Form.Memo2.Lines.Add(S);
end;
Used Delphi XE2 and there is no way to make this code running and login. It's same with XE3 demo. As I said, I'm really tired searching some solution, waste days into it and nothing.
Please guys, some help here. Really need it.

Try something like this:
function 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://www.filestrum.com');
Request.Add('login=example');
Request.Add('password=example');
IdHTTP := TIdHTTP.Create;
try
IdHTTP.AllowCookies := True;
IdHTTP.HandleRedirects := True;
IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
IdHTTP.Post('http://www.filestrum.com/', Request, Response);
Result := IdHTTP.Get('http://www.filestrum.com/?op=my_account');
finally
IdHTTP.Free;
end;
finally
Request.Free;
end;
finally
Response.Free;
end;
except
on E: Exception do
ShowMessage(E.Message);
end;
end;

Related

HTTPS post not working

I'm doing a HTTPS post with this code to azurewebsites.
http://MYAPP.azurewebsites.net/api/MYFUNC
I'm currently using this code:
procedure TForm1.OriginalTest();
var
lHTTP: TIdHTTP;
HTTPResult: string;
RequestBody: TStream;
URL: String;
Body: string;
IOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
lHTTP := TIdHTTP.Create;
try
Body := '{}';
RequestBody := TStringStream.Create(Body, TEncoding.UTF8);
lHTTP.Request.Accept := '';
lHTTP.Request.UserAgent := '';
lHTTP.Request.CustomHeaders.Add('x-functions-key:<your api key>');
lHTTP.ConnectTimeout := 24000;
lHTTP.ReadTimeout := 24000;
IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
IOHandler.SSLOptions.Method := sslvTLSv1_2;
lHTTP.IOHandler := IOHandler;
try
URL := 'https://<yourapp>.azurewebsites.net/api/<funcname>';
HTTPResult := lHTTP.Post(url, RequestBody);
Memo1.Lines.Add(HTTPResult);
except
on E:Exception do
begin
Memo1.Lines.Add(Format('Error sending data. Error: %s', [E.Message] ));
end;
end;
finally
lHTTP.Free;
RequestBody.Free;
end;
end;
For whatever reason, this code gives me the following error:
Error sending data. Error: Socket Error # 10054 Connection reset by peer.
I tried making a simple HTTPS Post using .NET with HttpWebRequest , and it works fine. What am I doing wrong here?
I just gave up and used WinApi.WinInet instead.

Login to secured website programmatically

I have setup the help for my Firemonkey app on a password-secured website hosted by site44.com.
Passing the password as part of the URL doesn't work with certain browsers because they consider it insecure. So I tried the following code. This doesn't work either. Function Login returns the login HTML of the website but does not actually log me in. Can you tell me where my code is wrong?
procedure TMyMainForm.OnlineHelp1Execute(Sender: TObject);
function Login: string;
var
IdHTTP: TIdHTTP;
Request: TStringList;
Response: TMemoryStream;
begin
Result := '';
try
Response := TMemoryStream.Create;
try
Request := TStringList.Create;
try
Request.Add('password=mypassword');
IdHTTP := TIdHTTP.Create;
try
IdHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
IdHTTP.AllowCookies := True;
IdHTTP.HandleRedirects := False;
IdHTTP.Post('https://mysite.site44.com/', Request, Response);
Result := IdHTTP.Get('https://mysite.site44.com/');
finally
IdHTTP.Free;
end;
finally
Request.Free;
end;
finally
Response.Free;
end;
except
on E: Exception do
ShowMessage(E.Message);
end;
end;
begin
ShowMessage(Login);
end;

Delphi idhttp long URL parameter is splittet automatically

I am using indys idhttp to submit an URL (post)
Procedure submit_post(url_string,EncodedStr:string;amemo:TMemo);
var
aStream: TMemoryStream;
Params: TStringStream;
begin
aStream := TMemoryStream.create;
Params := TStringStream.create('');
try
with Fmain.IdHTTP1 do
begin
Params.WriteString(EncodedStr);
Request.ContentType := 'application/x-www-form-urlencoded';
Request.Charset := 'utf-8';
try
Response.KeepAlive := False;
Post(url_string, params, aStream);
except
on E: Exception do
begin
Screen.Cursor := crDefault;
exit;
end;
end;
end;
aStream.WriteBuffer(#0' ', 1);
aStream.Position := 0;
amemo.Lines.LoadFromStream(aStream);
Screen.Cursor := crDefault;
finally
aStream.Free;
Params.Free;
end;
end;
It works like a charm for me. I am trying to submit a URL (post) with a parameter containing 300 chars, but will be splittet automatically by adding an "&" every 90 chars. So the server only receives 90 chars instead of 300.
How can I submit an URL with a 300 character parameter without this automatic separation ?
function SubmitPost(Params:String): string;
const
URL= 'http://xxxx.com/register.php?';
var
lHTTP: TIdHTTP;
Source,
ResponseContent: TStringStream;
I:Integer;
begin
lHTTP := TIdHTTP.Create(nil);
lHTTP.Request.ContentType := 'text/xml';
lHTTP.Request.Accept := '*/*';
lHTTP.Request.Connection := 'Keep-Alive';
lHTTP.Request.Method := 'POST';
lHTTP.Request.UserAgent := 'OS Test User Agent';
Source := TStringStream.Create(nil);
ResponseContent:= TStringStream.Create;
try
try
lHTTP.Post(URL+Params, Source, ResponseContent);
Result := ResponseContent.DataString;
except
//your exception here
end;
finally
lHTTP.Free;
Source.Free;
ResponseContent.Free;
end;
end;
Usage
mmo1.Text := SubmitPost('Username=xxxx&Password=xxxx');
I found the mistake. My Post function works perfectly, but the URL is built by params coming from a memo line. With "WantReturns = FALSE", I can build a URL with the maximum line length of the memo. I guess 1024 characters per line which is okay for me.

Upload image to imageshack with delphi

I have the API key and read the sparse documentation on their site, but still having trouble getting this to work so if anyone has any examples they could share then that would be great. I do not need to worry about videos or anything fancy, just a simple upload with the return information will suit my needs.
uses IdHttp;
function PostData:string;
var
url: string;
text: string;
http: TIDHttp;
valid: boolean;
param: TStringList;
begin
http := TIDHttp.Create(nil);
http.HandleRedirects := true;
http.ReadTimeout := 5000;
param := TStringList.create;
param.Clear;
param.Add('fileupload=c:\image.png');
param.Add('key=MY_API_KEY');
param.Add('tags=tag1,tag2');
valid := true;
url := 'http://www.imageshack.us/upload_api.php';
try
text := http.Post(url, param);
except
valid := false;
end;
if valid then
PostData := text
else
PostData := '';
end;
Thx.
Kevin
I pretty much did the exact same thing last nite. Thx tho.
procedure TForm1.Button1Click(Sender: TObject);
var
MPData: TIdMultiPartFormDataStream;
sResponse: string;
begin
MPData := TIdMultiPartFormDataStream.Create;
MPData.AddFile('fileupload','c:\image.png','image/png');
MPData.AddFormField('tags','testfile,flyasia');
MPData.AddFormField('public','no');
MPData.AddFormField('key','API_KEY_HERE');
sResponse := IdHTTP1.Post('http://www.imageshack.us/upload_api.php', MPData);
MPData.Free;
Memo1.Text := sResponse;
end;

delphi 2010 - tidhttp post

i want to post data to the following url:
http://mehratin.heroku.com/personals/new
i write the following code but has problem:
procedure TForm1.Button3Click(Sender: TObject);
var
aStream: TMemoryStream;
Params: TStringList;
begin
aStream := TMemoryStream.Create;
Params := TStringList.Create;
try
with IdHTTP1 do
begin
Params.Add('fname=123');
Params.Add('lname=123');
Request.ContentType := 'application/x-www-form-urlencoded';
try
Response.KeepAlive := False;
Post('http://localhost:3000/personals/new', Params);
except
on E: Exception do
showmessage('Error encountered during POST: ' + E.Message);
end;
end;
how can i post data by TIDHtttp.post method in delphi 2010?
First things first, you'd need to read the http response code (it would have been useful to include that in your question).
In the absence of that I've used the Indy http object before as shown below. I included the parameters in my URL though. To troubleshoot, try running this with an http.Get to ensure the port is open, and you can actually connect to the server. Here's my example for completenes:
// parameters
params := format('export=1&format=%s&file=%s', [_exportType, destination]);
// First setup the http object
procedure TCrystalReportFrame.SetupHttpObject();
begin
try
IDHTTP1.HandleRedirects := TRUE;
IDHTTP1.AllowCookies := true;
IDHTTP1.Request.CacheControl := 'no-cache';
IdHTTP1.ReadTimeout := 60000;
_basePath:= GetBaseUrl;
except
on E: Exception do
begin
Global.LogError(E, 'SetupHttpObject');
end;
end;
end;
// Then have an onwork event
procedure TCrystalReportFrame.HttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
var
Http: TIdHTTP;
ContentLength: Int64;
Percent: Integer;
begin
Http := TIdHTTP(ASender);
ContentLength := Http.Response.ContentLength;
end;
// The actual process
procedure TCrystalReportFrame.ProcessHttpRequest(const parameters: string);
var
url : string;
begin
try
try
SetupHttpObject;
IdHTTP1.OnWork:= HttpWork;
url := format('%s&%s', [_basePath, parameters]);
url := IdHTTP1.Post(url);
except
on E: Exception do
begin
Global.LogError(E, 'ProcessHttpRequest');
end;
end;
finally
try
IdHTTP1.Disconnect;
except
begin
end;
end;
end;
end;

Resources