我在使用 poco 时遇到了一个奇怪的问题。我可以很好地构建它,并将其链接到测试应用程序。但是,当我下载一个 url 时,无论我使用什么 url,它都会报告 HostNotFound 异常。该文件可以在任何地方的隐身浏览器中访问,并且可以在 dns 中解析....我有点不知所措...有什么想法吗?
//机器上的 dns 显示错误 nslookup s3.amazonaws.com 服务器:未知 地址:192.168.0.1
非权威回答: 名称:s3-1.amazonaws.com 地址:72.21.215.196 别名:s3.amazonaws.com s3.a-geo.amazonaws.com
// calling helper
CString host("http://s3.amazonaws.com");
CString path("/mybucket.mycompany.com/myfile.txt");
CString errmsg;
CString data = GetURL(host,path,errmsg);
// poco helper code
CString GetURL(CString host, CString path_query, CString &debmsg)
{
debmsg = CString("");
try
{
// convert request
std::string tmphost((LPCTSTR)host);
std::string tmppath((LPCTSTR)path_query);
// creation session and request
HTTPClientSession session(tmphost,80);
// disable proxy
session.setProxyHost("");
HTTPRequest req(HTTPRequest::HTTP_GET,tmppath,HTTPMessage::HTTP_1_1);
// send request
session.sendRequest(req);
// get response
HTTPResponse res;
std::istream * response = &session.receiveResponse(res);
// convert it back to mfc string
streambuf *pbuf = response->rdbuf();
std::ostringstream ss;
ss << pbuf;
CString data(ss.str().c_str());
return data;
}
catch (Poco::Exception& ex)
{
CString err(ex.displayText().c_str());
debmsg.Format("error getting url: %s%s err: %s",host,path_query,err);
}
return CString("<error>");
}
最佳答案
刚刚遇到了类似的问题。请注意,您的主机名是 "http://s3.amazonaws.com"
。
主机 的实际名称是“s3.amazonaws.com”
。 "http://"
部分指定了协议(protocol)。 HTTPClientSession
类无论如何只能用于 http 协议(protocol)。
在我的例子中,剥离 “http://”
并仅使用实际的主机名工作正常:“s3.amazonaws.com”
:
HTTPClientSession session("s3.amazonaws.com");
(好吧,在我的例子中是 "http://ws.audioscrobbler.com"
,但这不是重点)。可能为时已晚,无法确定这是否真的是您问题的答案,该错误看起来确实与我的有所不同,但希望它可以帮助像我一样通过搜索到达这里的人。
关于visual-c++ - poco httpclientsession 给出主机未找到错误,即使主机已解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19776324/