无法发送正确的休息请求

时间:2013-12-10 20:49:43

标签: rest vmware d libcurl

我想通过rest api连接到vCloud Dirrector。 我正确的登录curl命令是

curl -i -k -H "Accept:application/*+xml;version=1.5" -u "administrator@System:qwe123" -X POST https://172.16.8.240/api/sessions

但是我不能把它写在D上。

import std.stdio;
import std.string;
import std.net.curl;

int main(string[] argv)
{
    auto httpClient = HTTP();
    httpClient.clearRequestHeaders();
    httpClient.addRequestHeader("Accept", "application/*+xml;version=1.5");
    httpClient.authenticationMethod = HTTP.AuthMethod.basic;
    httpClient.setAuthentication("administrator", "System:qwe123");
    try
    {
        auto a = post("https://172.16.8.240/api/sessions", "", httpClient);
    }
    catch (CurlException ex)
    {
        writeln(ex);
    }

    return 0;
}

我有CurlException

std.net.curl.CurlException@std\net\curl.d(3504): Peer certificate cannot be auth
enticated with given CA certificates on handle 1D53758
----------------
0x00409456 in stdexceptionenforceEx at C:\D\dmd2\src\phobos\std\exception.d(527)

0x0043D88A in stdnetcurlCurl_check
0x00408742 in stdnetcurlpost at C:\D\dmd2\src\phobos\std\net\curl.d(423)
0x004020D4 in D at C:\Users\yrybalka\documents\visual studio 2013\Projects\visua
lStudioD\visualStudioD\main.d(15)
0x0042E670 in rtdmain2_d_run_mainrunAll__lambda1
0x0042E643 in rtdmain2_d_run_mainrunAll
0x0042E55B in _d_run_main
0x0042E378 in main at C:\Users\yrybalka\documents\visual studio 2013\Projects\vi
sualStudioD\visualStudioD\SuperClass.d(3)
0x0046C2C9 in mainCRTStartup
0x76CA495D in BaseThreadInitThunk
0x778D98EE in RtlInitializeExceptionChain
0x778D98C4 in RtlInitializeExceptionChain

也许我应该为https使用一些编译器密钥?或者我的代码错了吗?

1 个答案:

答案 0 :(得分:3)

至少是一种解决方法,如果你添加了

httpClient.verifyPeer = false;
httpClient.verifyHost = false;

在执行请求之前,它应该使此错误无效(通过不实际检查证书)。我不知道如何添加您的根证书,也许其他人可以回答。