
我如何从我的C ++程序打开一个URL?
在ruby你可以做
%x(open https://Google.com)
C ++中的等价物是什么? 我想知道是否有一个平台无关的解决scheme。 但是,如果没有,我想更好的Unix / Mac
这是我的代码:
C ++应用程序崩溃后速度慢(MSVS,Win8 x64)
跟踪程序中的所有调用?
什么是不调用libusb_exit()的后果
closures“blah.exe已停止工作”消息。 C#
在删除之前从共享内存中分离
#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; }
pthread_create在编译时返回错误
WebClIEnt在windows 8.1中下载损坏的文件
重复udp数据包:发生多less次?
如何延迟closures/重新启动/hibernate在windows 7或windows 8在VC + +
C ++中面向对象的信号量的使用
使用libcurl ,这里是一个简单的例子 。
编辑 :如果这是关于从C ++启动Web浏览器,您可以在system上调用一个shell命令与系统:
system("<mybrowser> http://Google.com");
用您想要启动的浏览器替换<mybrowser> 。
你的问题可能意味着两件事情。 1)用浏览器打开网页。
ShellExecute(0,L"http://www.Google.com",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<<"nn"; 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.1rnHost: "); strcat(get_http,url); strcat(get_http,"rnConnection: closernrn"); 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,SW_SHOWnorMAL); }
ps它使用WinAPI,如果我是正确的。 所以它不是多平台解决方案。
创建一个函数并使用Software_Developer已经提到的winsock复制代码。
例如:
#ifdef _WIN32 // this is required only for windows if (WSAStartup(MAKEWORD(2,&wsaData) != 0) { //... } #endif
这里的winsock代码
#ifdef _WIN32 WSACleanup(); #endif
总结以上是内存溢出为你收集整理的从C ++代码打开URL全部内容,希望文章能够帮你解决从C ++代码打开URL所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)