从C ++代码打开URL

我如何从我的C ++程序打开一个URL?

在ruby你可以做

%x(open https://google.com) 

C ++中的等价物是什么? 我想知道是否有一个平台无关的解决scheme。 但是,如果没有,我想更好的Unix / Mac

这是我的代码:

 #include <stdio.h> #include <string.h> #include <fstream> int main (int argc, char *argv[]) { char url[1000] = "https://www.google.com"; std::fstream fs; fs.open(url); fs.close(); return 0; } 

使用libcurl ,这里是一个简单的例子 。

编辑 :如果这是关于从C ++启动Web浏览器,您可以在system上调用一个shell命令与系统:

 system("<mybrowser> http://google.com"); 

用您想要启动的浏览器替换<mybrowser>

你的问题可能意味着两件事情。 1)用浏览器打开网页。

 ShellExecute(0, 0, L"http://www.google.com", 0, 0 , SW_SHOW ); 

这应该工作,它与相关的程序打开文件。 应该打开浏览器,通常是默认的。 2)获取网页的代码,你会自己渲染或做一些其他的事情。 为此我想阅读这个或/和这个

我希望这至少有一点帮助。

编辑:没有注意到,你要求的UNIX,这只适用于Windows。

C不像您提到的脚本语言那么高级。 但是,如果你想远离基于套接字的编程,请尝试卷曲。 卷曲是一个伟大的C库,有许多功能。 我已经使用了多年,总是推荐它。 它还包括一些独立的测试或外壳使用程序。

这里是使用winsock的windows代码示例。

 #include <winsock2.h> #include <windows.h> #include <iostream> #include <string> #include <locale> #pragma comment(lib,"ws2_32.lib") using namespace std; string website_HTML; locale local; void get_Website(char *url ); int main () { //open website get_Website("www.google.com" ); //format website HTML for (size_t i=0; i<website_HTML.length(); ++i) website_HTML[i]= tolower(website_HTML[i],local); //display HTML cout <<website_HTML; cout<<"\n\n"; return 0; } //*************************** void get_Website(char *url ) { WSADATA wsaData; SOCKET Socket; SOCKADDR_IN SockAddr; int lineCount=0; int rowCount=0; struct hostent *host; char *get_http= new char[256]; memset(get_http,' ', sizeof(get_http) ); strcpy(get_http,"GET / HTTP/1.1\r\nHost: "); strcat(get_http,url); strcat(get_http,"\r\nConnection: close\r\n\r\n"); if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { cout << "WSAStartup failed.\n"; system("pause"); //return 1; } Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); host = gethostbyname(url); SockAddr.sin_port=htons(80); SockAddr.sin_family=AF_INET; SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr); cout << "Connecting to "<< url<<" ...\n"; if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0) { cout << "Could not connect"; system("pause"); //return 1; } cout << "Connected.\n"; send(Socket,get_http, strlen(get_http),0 ); char buffer[10000]; int nDataLength; while ((nDataLength = recv(Socket,buffer,10000,0)) > 0) { int i = 0; while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') { website_HTML+=buffer[i]; i += 1; } } closesocket(Socket); WSACleanup(); delete[] get_http; } 

我使用ShellExecuteA()有更好的运气。 我听说使用“system()”时存在很多安全风险。 这就是我为自己的代码所提出的。

 void SearchWeb( string word ) { string base_URL = "http://www.bing.com/search?q="; string search_URL = "dummy"; search_URL = base_URL + word; cout << "Searching for: \"" << word << "\"\n"; ShellExecuteA(NULL, "open", search_URL.c_str(), NULL, NULL, SW_SHOWNORMAL); } 

ps它使用WinAPI,如果我是正确的。 所以它不是多平台解决方案。

创建一个函数并使用Software_Developer已经提到的winsock复制代码。

例如:

 #ifdef _WIN32 // this is required only for windows if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { //... } #endif 

这里的winsock代码

 #ifdef _WIN32 WSACleanup(); #endif