Hello listers,
Using Synapse, the developer has very good features to deal with the HTTP protocol. But imagine you want only to do a "get" in a URL and take a string back. I imagine it can be done with the standard units of Freepascal. Is it true? How could I do it with FPC 2.7.1? Best regards,
Luciano
Mailing lists / fpc-pascal / 2012-11
A simple HTTP request with FPC standard units
18 messages · 7 people · –
- Ssilvioprog 5
- Lluciano de souza 4
- MMichael Van Canneyt 4
- LLeonardo M. Ramé 2
- PPaul Breneman 1
- RReinier Olislagers 1
- +1 more
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
luciano de souza wrote 5 lines
very simple:
uses fphttpclient;
Var
S : String;
begin
With TFPHttpClient.Create(Nil) do
try
S:=Get(ParamStr(1));
finally
Free;
end;
Writeln('Got : ',S);
end.home: >fpc -S2 th.pp
/usr/bin/ld: warning: link.res contains output sections; did you forget -T? home: >th http://www.freepascal.org/
Got : <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>
<!-- Web Page Design by James Koster - http://www.jameskoster.co.uk and Marko Mihel?i? - http://www.mcville.net-->
<head>
I cut off the rest.
Michael.
Yes, when I ask for a simple way, I could not imagine something so simpler! Thank you. Your tip help me a lot.
The success of your answer is so that I add a question: is there also a FPC native unit dealing with SMTP?
Michael Van Canneyt wrote 45 lines
very simple: uses fphttpclient; Var S : String; begin With TFPHttpClient.Create(Nil) do…
GREAT unit, I love Free Pascal!
Two errors, with:
================
program project1;
{$mode objfpc}{$H+}
uses
fphttpclient;
var
S: string;
begin
with TFPHttpClient.Create(nil) do
try
S := Get(ParamStr(1));
finally
Free;
end;
WriteLn('Got : ', S);
end.
================I got:
================
C:\Users\silvioprog\Desktop\test>project1.exe
An unhandled exception occurred at $004115F1:
EHTTPClient: Invalid protocol : ""
$004115F1
$0041189E
$004118FE
$0040167F main, line 13 of project1.lpr
================
With:
================
program project1;
{$mode objfpc}{$H+}
uses
fphttpclient;
var
S: string;
begin
with TFPHttpClient.Create(nil) do
try
S := Get('http://silvioprog.com.br');
finally
Free;
end;
WriteLn('Got : ', S);
end.
================I got:
================
C:\Users\silvioprog\Desktop\test>project1.exe
An unhandled exception occurred at $0040FE72:
EAccessViolation: Access violation
$0040FE72
$00410042
$00411576
$004117CE
$0041182E
$0040166D main, line 13 of project1.lpr
================
What I'm is wrong?
My environment:
Lazarus 1.1 r38836 FPC 2.7.1 i386-win32-win32/win64
2012/11/23 Michael Van Canneyt
Show quoted text 41 lines
I solved the error. In:
fphttpclient.pas:line285
Function TFPCustomHTTPClient.GetServerURL(URI : TURI) : String;
Var
D : String;
begin
D:=URI.Path;
If (D[1]'/') then
D:='/'+D;
If (D[Length(D)]'/') then
D:=D+'/';
Result:=D+URI.Document;
if (URI.Params'') then
Result:=Result+'?'+URI.Params;
end;I changed to:
Function TFPCustomHTTPClient.GetServerURL(URI : TURI) : String;
Var
D : String;
begin
D:=URI.Path;
If Length(D) = 0 then
D := '/';
If (D[Length(D)]'/') then
D:=D+'/';
Result:=D+URI.Document;
if (URI.Params'') then
Result:=Result+'?'+URI.Params;
end;And worked fine:
==============
C:\Users\silvioprog\Desktop\test>project1.exe
Got : <!DOCTYPE html>
<!--[if lt IE 7 ]> <html lang="pt-br" class="no-js ie6"> <![endif]--> <!--[if IE 7 ]> <html lang="pt-br" class="no-js ie7"> <![endif]--> <!--[if IE 8 ]> <html lang="pt-br" class="no-js ie8"> <![endif]--> <!--[if IE 9 ]> <html lang="pt-br" class="no-js ie9"> <![endif]--> <!--[if (gt IE 9)|!(IE)]><!--><html lang="pt-br"
class="no-js"><!--<![endif]-->
<html>
<head>
<!--[if ie]><meta http-equiv="X-UA-Compatible"
content="IE=edge,chrome=1"><![end
if]-->
<title>silvioprog.com.br - Home</title>
<meta charset="UTF-8">
<meta name="author" content="Silvio Clecio - ">
<meta name="keywords" content="silvioprog, lazarus, freepascal, fpweb, cgi, laza
rus cgi, css, ajax, javascript, dom, desenvolvimento web, padroes web, internet
movel, mobilidade, microformats, xhtml, html5">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="Bem-vindo ao espaço do desenvolvedor livre!">
<meta name="generator" content="LazSolutions">
<link rel="author" href="humans.txt" type="text/plain"> <link rel="shortcut icon" href="favicon.ico">
<link rel="apple-touch-icon" href="apple-touch-icon.png"> <link rel="shortlink" href="http://tinyurl.com/silvioprog"> <!--<link rel="stylesheet" href="css/style.css">--> <!--[if lt IE 8]><script src="js/ie6update.js"></script><![endif]--> <!--<script src="js/jquery.min.js"></script>-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17508381-4']);
_gaq.push(['_setDomainName', '.com.br']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript';ga.a
sync = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : ' http://w
ww') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefor
e(ga, s);
})();</script>
<style>
body { text-align: center;}
h1 { font-size: 50px; text-align: center }
span[frown] { transform: rotate(90deg); display:inline-block; color: #bbb; }
body { font: 20px Constantia, 'Hoefler Text', "Adobe Caslon Pro", Baskerville
, Georgia, Times, serif; color: #999; text-shadow: 2px 2px 2px rgba(200, 200, 20
0, 0.5); }
::-moz-selection{ background:#FF5E99; color:#fff; } ::selection { background:#FF5E99; color:#fff; }
article {display:block; text-align: left; width: 500px; margin: 0 auto; }
a { color: rgb(36, 109, 56); text-decoration:none; } a:hover { color: rgb(96, 73, 141) ; text-shadow: 2px 2px 2px rgba(36, 109, 56,
0.5); }
</style>
</head>
<body>
<article>
<h1>silvioprog.com.br</h1>
<div>
<p>Olá, caro visitante,</p>
<p>Estamos importando todo o material do programmer ObjectPascal para e
ste novo espaço, aguarde.</p>
<ul>
</ul>
</div>
</article>
</body>
</html>
==============
I'll open an issue in bugtracker, please wait me...
2012/11/23 silvioprog
Show quoted text 127 lines
Silvio Clécio
My public projects - github.com/silvioprog
Done:
http://bugs.freepascal.org/view.php?id=23372
2012/11/23 silvioprog
Show quoted text 258 lines
Silvio Clécio
My public projects - github.com/silvioprog
Show quoted text 41 lines
The code needed to execute the function could be even smaller if this is changed to a class function. Instead of needing to create an instance, then using, then freeing, the whole thing could something like this:
...
begin
s := TFPCustomHTTPClient.GetServerURL('http://a_site/a_page');
writeln(s);
end;Leonardo M. Ramé wrote 50 lines
You mean probably
s := TFPCustomHTTPClient.Get('http://a_site/a_page');It could be done, but it will need to create an instance anyway. Although I suspect such a simple case is a minority.
Michael.
----- Original Message -----
Show quoted text 74 lines
Yes, I said this because the original poster wrote he needed the very little code to accomplish this.
Leonardo M. Ramé
http://leonardorame.blogspot.com
Leonardo M. Ramé wrote 13 lines
I implemented the class methods for all forms of Get/Post/Formpost, so now you can do:
s := TFPCustomHTTPClient.SimpleGet('http://a_site/a_page');You can't get more simple than that.
Michael.
Among its hundreds of units, the Freepascal always keeps surprises for us. FPHTTPClient was a pleasant surprise.
The talk has followed freely and perhaps my question has been skipped: is there Freepascal native routines to deal with SMTP protocol? In other words, is there a native way to send an e-mail with Freepascal?
2012/11/23, Michael Van Canneyt :
Show quoted text 27 lines
luciano de souza wrote 5 lines
Here is the most simple way I know of (but I'd be glad to learn of a more simple way):
http://www.turbocontrol.com/easyemail.htm
very simple: uses fphttpclient; Var S : String; begin With TFPHttpClient.Create(Nil) do…
2012/11/23 Michael Van Canneyt
Show quoted text 41 lines
Really TFPHttpClient is a pleasant class. Now I updated EasyRSS plugin for use it.
Thank you again Michael! :)
Among its hundreds of units, the Freepascal always keeps surprises for us. FPHTTPClient…
luciano de souza wrote 5 lines
There are no FPC-provided native freepascal routines for this, but I see Paul Breneman has provided an alternative.
Contributions are welcome.
It is mostly lack of time that keeps us from implementing these things. As far as I know, SMTP is a fairly simple protocol, probably it is not too hard to implement.
Michael.
Show quoted text 38 lines
Unfortunately, I don't have enough knowledlge even to start a task like that. But observing the firm journey of Freepascal, I am sure we will have it soon.
Michael Van Canneyt wrote 64 lines
2012/11/24 Luciano de Souza
Show quoted text 25 lines
I have some experience with the SMTP protocol. Reading the RFC, I can understand better its requirements and I will able to implement a TFPSMTPClient. What do you think about?
silvioprog wrote 16 lines
Nice idea ;)
AFAIR, the Synapse demo/contributed applications on their web/FTP site also has SMTP examples, which might be useful in taking inspiration from.
Reinier Olislagers wrote 7 lines
.
.
Show quoted text 9 lines
As far as I know, lnet (available as an independent project in FPC svn repository - http://svn.freepascal.org/svn/fpcprojects/lnet/) also includes SMTP support.
Tomas
