Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Can someone convert there php function to Delphi ? (about sock)

Can someone convert there php function to Delphi, please help me

function get_url($url){

$url_parsed = parse_url($url);
$host = $url_parsed["host"];
$port = $url_parsed["port"];
if (($port==0)||(!isset($port)))
      $port = 80;
$path = $url_parsed["path"];

if ($url_parsed["query"] != "")
$path .= "?".$url_parsed["query"];

$out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";

$fp = fsockopen($host, $port, $errno, $errstr, 30);
fwrite($fp, $out);

fclose($fp);

}
0
ngocquynh85
Asked:
ngocquynh85
  • 3
  • 3
1 Solution
 
ZhaawZSoftware DeveloperCommented:
Do you want to use some component (for example - TClientSocket) or WinAPI functions?
0
 
ngocquynh85Author Commented:
if need. :)
I want to use sock to open one page.
then use sock to read source of that page.
0
 
ngocquynh85Author Commented:
anyone can help me ???
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ZhaawZSoftware DeveloperCommented:
uses
  winsock;

function GetHTML(addr: pchar) : string;
var
  sock : integer;
  n : integer;
  tstadr : sockaddr_in;
  data : wsadata;
  buf : pchar;
begin
result := '';
if WSAStartup(1, data) = 0 then begin
  sock := socket(AF_INET, SOCK_STREAM, 0);
  if sock <> -1 then begin
    with tstadr do begin
      fillchar(sin_zero, sizeof(sin_zero), 0);
      sin_family := AF_INET;
      sin_port := htons(80);
      sin_addr.S_addr := inet_addr(addr);
    end;
    n := connect(sock, tstadr, Sizeof(tstadr));
    if n = 0 then begin
      getmem(buf, 1024);
      fillchar(buf[0], 1024, 0);
      lstrcat(buf, 'GET / HTTP/1.0'#10);
      lstrcat(buf, 'Host: ');
      lstrcat(buf, addr);
      lstrcat(buf, #10#10);
      send(sock, buf[0], lstrlen(buf), 0);
      repeat
        fillchar(buf[0], 1024, 0);
        n := recv(sock, buf[0], 1024, 0);
        result := result + buf;
      until n = 0;
      freemem(buf);
    end;
  end;
  WSACleanUp;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Text := GetHTML('205.188.244.138')
end;
0
 
ngocquynh85Author Commented:
thank ZhaawZ , but when I try it, it didn't work :|
in line  
   n := connect(sock, tstadr, Sizeof(tstadr));
      if n = 0 then begin
...
n alway is -1.
I don't know why ?
0
 
ZhaawZSoftware DeveloperCommented:
Do you use IP address (not a host) as addr in function?
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now