0

I want to download a page from a website.

The page is located at an url like: http://www.example.com/status?param1=19439016&param2=812554&param3=140104010101

For download I use the code (downloaded from the internet):

function Download_HTM(const sURL, sLocalFileName:string): boolean;
begin
  Result:=True;
  with TDownLoadURL.Create(nil) do
  try
    URL:=sURL;
    Filename:=sLocalFileName;
    try
      ExecuteTarget(nil);
    except
      Result:=False
    end;
  finally
    Free;
  end;
end;

my call is:

 sURL := Format('%s/status?param1=%s&param2=%s&param3=%s',
             [sWeb, param1, param2, param3]);
 if Download_HTM(sURL, sLocalFile) then 
 ....

but don't work. Also I used:

 sURL := Format('%s/status?param1=%s'#38'param2=%s'#38'param3=%s',
             [sWeb, param1, param2, param3]);

but don't work. I can't insert the & character in the url. Any advice?

Now, instead of Download_HTM I tried the bellow function (from a stackoverflow example):

function GetURLAsString(const aURL: string): string;
var
  lHTTP: TIdHTTP;
begin
  lHTTP := TIdHTTP.Create(nil);
  try
    Result := lHTTP.Get(aURL);
  finally
lHTTP.Free;
  end;
end;

but I receive: Socket error 10014 - Bad address I'm sure that my address is corect because when I put it on the internet explorer I receive the corect html page.

When I show the sURL variable with the command ShowMessage(sURL) I can see the entire url but without the & character and the character p from the param3 word is underlined (the p character follow after the last & character from the URL) and the behavoir same to be like when I use & to create a shoertcut on a menu caption !

sorin
  • 23
  • 1
  • 6
  • 3
    Try including the scheme (`http://`) to make it a valid URL. – Ondrej Kelle Jan 03 '14 at 13:27
  • 1
    Also, URL-encode the parameters, e.g. using the `TIdURI` class from Indy. – Ondrej Kelle Jan 03 '14 at 13:34
  • I don't knew how ! if I try to put http:// then when I try to save may question I receive an error mesage. – sorin Jan 03 '14 at 13:47
  • @Arioch. The error is about the question editing: Body cannot contain http://..., not about my url problem. – sorin Jan 03 '14 at 14:08
  • Text body can not, but code samples should be different story. Insert http into the code source, along with all other values and types of all Variables. SSCCE.org – Arioch 'The Jan 03 '14 at 14:09
  • @Arioch your link is not about how to use the & in the url. – sorin Jan 03 '14 at 14:10
  • Also "but don't work" conveys zero information alas. http://www.catb.org/~esr/faqs/smart-questions.html#beprecise – Arioch 'The Jan 03 '14 at 14:12
  • My link about many methods and libraries to make http request and the usually have direct support for adding parameters – Arioch 'The Jan 03 '14 at 14:13
  • Why do you think you can't insert an ampersand in your URL? It's not a special character in Delphi strings, so it should just work. What fails when you try doing the obvious? – Rob Kennedy Jan 03 '14 at 14:15
  • ok.Sorry for my English. My problem is that I can't send the parameters on the url. I wil try your link now – sorin Jan 03 '14 at 14:18
  • When running `ExecuteTarget(nil);` there probably happens an error. Show full text including the class of that error after you would show the variables types and values – Arioch 'The Jan 03 '14 at 14:21
  • @Rob Kennedy. I receive an error that say that the url: mysite.com/status?param1=xyzparam2=yzqparam3=abc is incorect. From this error I understand that the url is incorect because between xyz and param2 I need an & character. Also between yzq and param3 I need again an & character – sorin Jan 03 '14 at 14:23
  • the error may be in URL but it also may be in the window showing error text, also without http scheme the URL surely is not correct. IF this library does not work for you - just use another. There are many. – Arioch 'The Jan 03 '14 at 14:52
  • I edited my question now, with another function for download – sorin Jan 03 '14 at 14:59
  • now I was able to write the http scheme in the question – sorin Jan 03 '14 at 15:02
  • @Arioch 'The. To be sure that my address is correct I used cut (from internet explorer) URL and paste (to Delphi IDE) on my source code, avoiding the Format function. Something like sURL := 'http://www.example.com/status?param1=19439016&param2=812554&param3=140104010101'; but the I receive the same socket error 11004. – sorin Jan 03 '14 at 15:08
  • same ? you reported a different error, malformed url – Arioch 'The Jan 03 '14 at 15:13
  • * with the command ShowMessage(sURL)* it does not show the exact value but a message. Use debug windows Evaluate and Modify. Better - show the code with data types and values assigned to those very variables – Arioch 'The Jan 03 '14 at 15:15
  • Google for "socket error 11004" shows you have no route to server http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx – Arioch 'The Jan 03 '14 at 15:18
  • @Arioch 'The. Yes you are right. In my evaluate window the sURL is correct. the code look like: sURL := 'http://check.example.com/status?param1=19439016&param2=812554&param3=20140104010101'; the url work ok on the internet explorer. with the Download_HTM(sURL) I receive the error that this address is incorect and with GetURLAsString(sURL) I receive the Socket error 11004 – sorin Jan 03 '14 at 15:23
  • @Arioch 'The. Ops! Again you are right. When I google for 11004 I founded "bad address" without details. – sorin Jan 03 '14 at 15:31
  • when you making comment read help link on the right to get the syntax how to put code, so it would be displayed verbatim. maybe msie uses proxy or whatever. time to debug tcp and http and firewall layers. – Arioch 'The Jan 03 '14 at 15:32
  • you have nothing to google - I gave you link to OFFICIAL page about this error – Arioch 'The Jan 03 '14 at 15:33
  • Sorry, my english is very bad. When I posted the first comment with Shocket error I googled for 11004 and I founded the simple form of the error - Bad address. Now I used your link ! Now I will try to find a way (or library) like in your first link to get the same result like from internet explorer. Thank you very much for your effort – sorin Jan 03 '14 at 15:46
  • @Arioch 'The. Again you are right. Was about firewall layers. Now work ok. Thank you again ! – sorin Jan 03 '14 at 16:17
  • i googled for "socket error 11004" and 2nd link was official doc. Just two more sensible words to copy - and search results turned much better ;-) – Arioch 'The Jan 03 '14 at 16:48
  • additionally, when you "download from internet" code with lines like "except Result:=False end;" it is almost always a very bad code or a "prove of concept" not intended for real use. Real code would not hide errors like that, but would do everything so the user would report to programmer as much info about error as possible. Code like that would make users from other countries call you and say "but don't work" and you won't have a slightest idea what and where went wrong and how to fix it. Code like that is awful, think twice before learning from it, not even copying it – Arioch 'The Jan 03 '14 at 16:55

0 Answers0