2015-09-08 41 views
0

以下代码在Internet选项中设置的代理环境中不起作用。 POCO库的例子不显示,怎么办。有没有解决方法? 我使用最新的POCO库如何在代理环境中使用Poco C++(使用HTTPStreamFactory)

using Poco::URIStreamOpener; 
using Poco::StreamCopier; 
using Poco::Path; 
using Poco::URI; 
using Poco::SharedPtr; 
using Poco::Exception; 
using Poco::Net::HTTPStreamFactory; 
using Poco::Net::FTPStreamFactory; 

class WebInitializer 
{ 
public: 
    WebInitializer() 
    { 
     HTTPStreamFactory::registerFactory(); 
     FTPStreamFactory::registerFactory(); 
    } 

    ~WebInitializer() 
    { 
     HTTPStreamFactory::unregisterFactory(); 
     FTPStreamFactory::unregisterFactory(); 
    } 
}; 

void Download(std::string host, std::string path, std::string targetFilePath) 
    { 
     WebInitializer webInitializer; 
     std::string sUrl = host + path; 
     URI uri(sUrl);   
     std::shared_ptr<std::istream> pStr(URIStreamOpener::defaultOpener().open(uri)); 
     ofstream outputfile; 
     outputfile.exceptions(std::ofstream::badbit | std::ofstream::failbit); 

     outputfile.open(targetFilePath, std::ios_base::binary); 
     StreamCopier::copyStream(*pStr.get(), outputfile); 
     outputfile.close(); 

    } 

回答

1

testcase

HTTPStreamFactory factory("myproxy", port); 
URI uri(host); 
std::auto_ptr<std::istream> pStr(factory.open(uri)); 
std::istream& rs = s.receiveResponse(response); 
ofstream outputfile; 
outputfile.open(targetFilePath, std::ios_base::binary); 
StreamCopier::copyStream(*pStr.get(), outputfile); 
+0

感谢亚历克斯,什么是“MYPROXY”在这里,我必须得到系统依赖于OS,将它重定向情景工作代理字符串?有没有假设默认身份验证代理? – maniappa

+0

“myproxy”是您的代理服务器。有关身份验证示例,请参见[testProxyAuth](https://github.com/pocoproject/poco/blob/develop/Net/testsuite/src/HTTPClientSessionTest.cpp#L262)。 – Alex

+0

如果我的应用程序在某个客户机器上运行,我需要将他们的代理服务器名称,我说得对吗?这可能适用于我的测试案例 – maniappa

-1

这是我对这个问题的解决方案。在这个uri_proxy类的实现中没有显示哪个是特定于操作系统的,可以很容易地实现。

int redirectiocount = 0; 
     bool found= false; 
     while (redirectiocount < 10 && !found) 
     { 
      HTTPClientSession session(resolvedURI.getHost(), resolvedURI.getPort()); 
      session.setProxy(uri_proxy.getHost(), uri_proxy.getPort()); 
      session.setTimeout(Poco::Timespan(400, 0)); 
      std::string path(resolvedURI.getPathAndQuery()); 
      if (path.empty()) path = "/"; 
      HTTPRequest request(HTTPRequest::HTTP_GET, path); 
      session.sendRequest(request); 
      HTTPResponse response; 
      std::istream& rs = session.receiveResponse(response); 
      bool moved = (response.getStatus() == HTTPResponse::HTTP_MOVED_PERMANENTLY || 
       response.getStatus() == HTTPResponse::HTTP_FOUND || 
       response.getStatus() == HTTPResponse::HTTP_SEE_OTHER || 
       response.getStatus() == HTTPResponse::HTTP_TEMPORARY_REDIRECT); 
      if (moved) 
      { 

       resolvedURI.resolve(response.get("Location")); 
       redirectiocount++; 
       //throw URIRedirection(resolvedURI.toString()); 
      } 
      else if (response.getStatus() == HTTPResponse::HTTP_OK) 
      { 
       found = true;    
       ofstream outputfile; 
       outputfile.open(outfilePath, std::ios_base::binary); 
       StreamCopier::copyStream(rs, outputfile); 
       outputfile.close(); 

      } 
      else 
      { 
       std::string error(response.getReason()); 
       std::string urlstring = "-" + uri.toString(); 
       error += urlstring; 
       throw Poco::SyntaxException(error); 
      } 
+0

如果可能的话,考虑添加代码块中使用的大多数变量的声明。尝试在放置代码时缩进代码。 – Ram

相关问题