Load image from URL in C++

六眼飞鱼酱① 提交于 2020-01-02 12:58:32

问题


I am using C++ and OpenCV and I'd like to load an image from password protected URL. I succeeded in loading image from URL using idea of this link which uses POCO library, but I do not know what should I do when I have to use username and password in order to access the URL.


回答1:


I'd say do what @StevenV said and try to encode the credentials in the URI.

If that doesn't work or you don't want to use that method you have to use the POCO HTTPClientSession class instead. Something like this:

URI uri(url);
HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest req(HTTPRequest::HTTP_GET, uri.getPathEtc(), HTTPMessage::HTTP_1_1);
HTTPBasicCredentials creds("username","password");
creds.authenticate(req);
session.sendRequest(req);
HttpResponse resp;
std::istream file = session.reveiveResponse(resp);
if(resp.getStatus() == HTTP_OK){
  //copy image from istream file here;
}


来源:https://stackoverflow.com/questions/19383270/load-image-from-url-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!