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.

有帮助吗?

解决方案

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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top