[fpc-pascal] A simple HTTP request with FPC standard units

silvioprog silvioprog at gmail.com
Fri Nov 23 18:31:06 CET 2012


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 - silvioprog at gmail.com">
<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 <silvioprog at gmail.com>

> 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 <michael at freepascal.org>
>
>>
>>
>> On Fri, 23 Nov 2012, luciano de souza wrote:
>>
>>  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?
>>>
>>
>> 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.
>
> --
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20121123/5c7e2eb9/attachment.html>


More information about the fpc-pascal mailing list