【发布时间】:2015-01-19 10:03:05
【问题描述】:
我创建了一个使用 C++/Delphi 中的 POST 方法调用 Web 服务的应用程序。它在 http 下的 localhost 中运行良好,但是当我在生产服务器中尝试 https 协议时,我得到“Socket Error #2”。我不知道这意味着什么。 Web 服务器正在为其他应用程序(不是 Rad Studio)提供 https 服务而没有问题。我使用的代码摘自一个网站:
std::auto_ptr<TIdHTTP> httpPtr( new TIdHTTP(NULL) );
TIdHTTP *Http = httpPtr.get();
try
{
TIdSSLIOHandlerSocketOpenSSL *SSL = new TIdSSLIOHandlerSocketOpenSSL( Http );
// configure SSL settings as needed...
Http->IOHandler = SSL;
Http->HandleRedirects = true;
Http->AllowCookies = false;
Http->ConnectTimeout = 10000;
Http->ReadTimeout = 10000;
Http->Request->BasicAuthentication = true;
Http->Request->Password = username;
Http->Request->Username = password;
Memo1->Text = Http->Post( "http://logs.domain.dk", json);
}
catch( const Exception &e )
{
Memo1->Lines->Add(e.ClassName());
Memo1->Lines->Add(e.Message);
return;
}
更新:
我添加了以下代码来查看来自 Fiddler 的消息。
IdHTTP1->ProxyParams->ProxyServer = "127.0.0.1";
IdHTTP1->ProxyParams->ProxyPort = 8888;
现在它工作正常(使用 Fiddler 的代理),但当然,这不是解决方案,因为用户将没有 Fiddler。 不知道是不是很重要,但是https服务器并没有使用正常的https端口。它使用 4001,但我在 url 中传递了这个值:
IdHTTP1->Post("https://myweb.com:4001/api/certificate", json);
发生了什么?我很困惑。
【问题讨论】:
-
能否请您发布确切的套接字错误编号。 msdn.microsoft.com/en-us/library/windows/desktop/…
-
调用 iHTTP1->Post() 后,引发异常。我唯一能得到的信息是 CLASSNAME: EIdSocketError, MESSAGE:Socket Error # 2. 怎样才能得到 Socket 错误号?
-
捕获
EIdSocketError而不是Exception并打印出异常的LastError。那是套接字错误代码。 -
完成。 LastError 值为 2。它与一般异常“Socket Error #2”的数量相同。遗憾的是 Windows Sockets Error Codes 文档没有提到 #2 代码。
-
2 在 Windows 上不是有效的套接字错误代码。您不应该看到 2 报告。这让我认为您使用的是旧版本的 Indy,在启用 SSL 时,该版本的套接字错误报告存在问题。这是很久以前修好的。你实际使用的是什么版本的 C++Builder 和 Indy?
标签: delphi c++builder indy10