HTTP 不是套接字网络请求 - D

HTTP not socket web requests - D

如何发出 GETPOST HTTP 请求?我找到了 Socket 解决方案,但就是全部了吗?

string host = "google.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 /search.php HTTP/1.1\r\nHost: google.com\r\n\r\n";

listener.send(req);

看看std.net.curl. It has get and post方法:

import std.net.curl;

auto content = get("d-lang.appspot.com/testUrl2");
// --
auto content = post("d-lang.appspot.com/testUrl2", [1,2,3,4]);

curl 无疑是​​一个很好的解决方案。但是,这会为您的项目添加新的依赖项,对吗?根据您从事的项目类型,我建议您使用 Adam Ruppe 的 arsd 模块,尤其是 http 模块(他也在开发 http2 模块)- https://github.com/adamdruppe/arsd/blob/master/http.d . Or, perhaps if you want a framework, then vibe.d is your best option (http://vibed.org) as it has a HTTP client too. There are two vibe.d related, web development oriented, books that I recommend, and they are listed on the following page: http://vibed.org/tutorials .

更新:Python 的请求包有一个很棒的端口,您应该尝试一下:https://github.com/ikod/dlang-requests .

您可以使用 hunt-http 库来请求每个网站 ::

获取示例:

import hunt.http;

import std.stdio;

void main()
{
    auto client = new HttpClient();

    auto request = new RequestBuilder().url("https://www.google.com").build();
    auto response = client.newCall(request).execute();

    if (response !is null)
    {
        writeln(response.getBody().asString());
    }
}