December 03, 2014 Re: curl: catching exception on connect. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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 |
Copyright © 1999-2021 by the D Language Foundation