curl: catching exception on connect.

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 3 10:22:57 PST 2014


On 12/02/2014 05:19 AM, Suliman wrote:
>
>> connect() sends a "CONNECT" request to the server, as defined by HTTP
>> [1]. This method is only used when you're working with proxies and the
>> like. What you most likely want, however, is a "GET" request. Use
>> get() for that.
>
> So what is the best way to check status server response (400, 404 etc)
> to send get request and try to parse response, or there is any better
> way (probably with another lib?)?

connect() uses the curl.HTTP struct, which is available to us as well:

   http://dlang.org/phobos/std_net_curl.html#HTTP

I added two lines to the first example there:

import std.net.curl, std.stdio;

pragma(lib, "curl");

void main()
{
     // Get with custom data receivers
     auto http = HTTP("dlang.org");
     http.onReceiveHeader =
         (in char[] key, in char[] value) { writeln(key ~ ": " ~ value); };
     http.onReceive = (ubyte[] data) { /+ drop +/ return data.length; };
     http.perform();

     const HTTP.StatusLine statusLine = http.statusLine();
     writefln("GET request completed with code %s", statusLine.code);
}

Sample output:

date: Wed, 03 Dec 2014 18:19:15 GMT
server: Apache/2.2.22 (FreeBSD) PHP/5.3.15 with Suhosin-Patch 
mod_ssl/2.2.22 OpenSSL/0.9.8x DAV/2
last-modified: Sun, 02 Nov 2014 06:38:55 GMT
etag: "3b438-4f9a-506da7be81dc0"
accept-ranges: bytes
content-length: 20378
content-type: text/html
GET request completed with code 200

Ali



More information about the Digitalmars-d-learn mailing list