Needing help with basic HTTP requests..
AlexMcP
derptyderp at hotmail.co.uk
Tue Jul 9 02:03:30 PDT 2013
Hi guys,
I've only been trying to learn D for a short while, and compared
to the other more popular programming languages, I've been able
to find very little helpful and updated documentation or
tutorials on it.
I basically need help getting data from a website, just the HTML
of a webpage, in this case a PHP page. I'm using the following
code which compiles fine, but when run it just encounters a
'Socket.ERROR' with a value of -1.
I'm not sure what I'm doing wrong, but any help or suggestions
would be appreciated. As I said, I'm quite new to D...
---
//readData.d
module main;
import std.stdio;
import std.socket;
import std.cstream;
int main(string[] args)
{
string host = "www.dprogramming.com";
ushort port = 80;
Socket listener = new TcpSocket;
assert(listener.isAlive);
listener.blocking = false;
listener.connect(new InternetAddress(host, port));
char[] msg;
char[] req = cast(char[]) "GET /internet.php HTTP/1.1\r\nHost:
www.dprogramming.com\r\n\r\n";
listener.send(req);
while (1)
{
int bytes;
char[1024] buf;
bytes = listener.receive(buf);
if (Socket.ERROR == bytes)
{
writeln("Connection Error ", Socket.ERROR);
listener.close();
din.readLine();
return 1;
}
else if (0 == bytes)
{
try
{
writeln("Connection to target lost.");
}
catch (SocketException)
{
writeln("Connection closed.");
}
listener.close();
din.readLine();
return 1;
}
else
{
writeln("Recieved ", bytes, "bytes from ",
listener.remoteAddress().toString(), ": \"", buf[0 .. bytes],
"\"");
}
}
}
---
- Alex.
More information about the Digitalmars-d-learn
mailing list