如何从我的C++程序中打开URL?
在红宝石中你可以做到
%x(open https://google.com)
什么是C++中的等价物?我想知道是否有一个独立于平台的解决方案.但如果没有,我更喜欢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;
}
ST3*_*ST3 27
你的问题可能意味着两件事.1)用浏览器打开网页.
ShellExecute(0, 0, L"http://www.google.com", 0, 0 , SW_SHOW );
这应该工作,它打开文件与相关的程序.应该打开浏览器,通常是默认的.2)获取网页代码,您将自己渲染或做其他事情.为此我想读这个或/和这个
我希望它至少有一点帮助.
编辑:没有注意到,你要求UNIX,这只适用于Windows.
rec*_*que 18
编辑:如果这是关于从C++启动Web浏览器,您可以system在POSIX系统上调用shell命令:
system("<mybrowser> http://google.com");
通过替换<mybrowser>您要启动的浏览器.
这是使用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;
}
我在 Windows 中遇到了完全相同的问题。
我注意到在OP 的 gist 中,他string("open ")在第 21 行使用,但是,通过使用它会遇到此错误:
'open' 不被识别为内部或外部命令
经过研究,我发现openMacOS 是打开东西的默认命令。在 Windows 或 Linux 上是不同的。
Linux:xdg-open <URL>
窗户:start <URL>
对于像我一样使用 Windows 的人,您可以使用以下内容:
std::string op = std::string("start ").append(url);
system(op.c_str());
| 归档时间: | 
 | 
| 查看次数: | 95017 次 | 
| 最近记录: |