2012-11-06 107 views
0

如何在C++中打开一个url,Objective-C有大量的例子,但我的应用程序不使用objective-c并以main()开头,并且都是c/C++ 。我正在使用URLSimpleDownload,但它不再工作(返回-50)。我不想打开网页或浏览器,我只需要从c/C++中打开一个网址即可。打开网址mac osx C++

回答

1

您可以参考几个您提到的NSURL示例,并使用等效的CFURL* API。注意:CFURLRef是一个NSURL*。所以你只需要找出相应的基于NSURL的实现使用的接口CFURL*

这种CF型是NS型的关系被称为“免费桥接”。

请注意,并非所有东西都会一对一映射,NS-API有很多便利/添加 - 最好将其视为CF-API之上的抽象层。

+0

我看到有一个NSString的stringWithContentsOfURL功能,这正是我需要的,但没有相应的CFString字符串函数为了它? – Rasterman

+0

创建cfdata中间件 – justin

0

你可以尝试下载并安装cURLpp(从代码neuropost):

// Edit : rewritten for cURLpp 0.7.3 
// Note : namespace changed, was cURLpp in 0.7.2 ... 
#include <curlpp/cURLpp.hpp> 
#include <curlpp/Easy.hpp> 
#include <curlpp/Options.hpp> 

// RAII cleanup 
curlpp::Cleanup myCleanup; 

// standard request object. 
curlpp::Easy myRequest; 

// Set the URL. 
myRequest.setOpt(new curlpp::options::Url(std::string("http://example.com"))); 

// Send request and get a result. 
// By default the result goes to standard output. 
// Here I use a shortcut to get it in a string stream ... 
std::ostringstream os; 
os << myRequest.perform(); 

string asAskedInQuestion = os.str();