Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从C++;WinINet的套接字_C++_Sockets_Wininet - Fatal编程技术网

C++ 从C++;WinINet的套接字

C++ 从C++;WinINet的套接字,c++,sockets,wininet,C++,Sockets,Wininet,我正在尝试在使用WinInet的代码中使用套接字生成等效的代码(我已经在Windows中了) 这就是我得到的: #include <winsock2.h> #include <windows.h> #include <iostream> #pragma comment(lib,"ws2_32.lib") using namespace std; int main (){ WSADATA wsaData; if (WSAStartup(MAKEW

我正在尝试在使用WinInet的代码中使用套接字生成等效的代码(我已经在Windows中了)

这就是我得到的:

#include <winsock2.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main (){
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
        cout << "WSAStartup failed.\n";
        system("pause");
        return 1;
    }
    SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    struct hostent *host;
    host = gethostbyname("www.google.com");
    SOCKADDR_IN SockAddr;
    SockAddr.sin_port=htons(80);
    SockAddr.sin_family=AF_INET;
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
    cout << "Connecting...\n";
    if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
        cout << "Could not connect";
        system("pause");
        return 1;
    }
    cout << "Connected.\n";
    send(Socket,"GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n", strlen("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n"),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') {
            cout << buffer[i];
            i += 1;
        }
    }
    closesocket(Socket);
        WSACleanup();
    system("pause");
    return 0;
}
#包括
#包括
#包括
#pragma注释(lib,“ws2_32.lib”)
使用名称空间std;
int main(){
WSADATA WSADATA;
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0){
cout h_addr);

cout@AlexK。我已经看到了,但它对我不起作用。你尝试过转换到
WinINet
吗?你实现了什么功能,产生的代码在哪里?@BlakeYarbrough我添加了code@AlexK.你能帮我吗?@AlexK。我已经看到了,但对我来说不起作用。你是否尝试转换成
WinINet
然而?你实现了什么功能?产生的代码在哪里?@BlakeYarbrough我添加了code@AlexK.你能帮助我吗?
#include "stdafx.h"
#include <Windows.h>
#include <WinInet.h>
#pragma comment (lib, "Wininet.lib")
#pragma comment (lib, "urlmon.lib")
// Defines
#define POST 1
#define GET 0

void Request(int Method, LPCSTR Host, LPCSTR url, LPCSTR header, LPSTR data)
{
    try {
        //Retrieve default http user agent
        char httpUseragent[512];
        DWORD szhttpUserAgent = sizeof(httpUseragent);
        ObtainUserAgentString(0, httpUseragent, &szhttpUserAgent);

        char m[5];

        if (Method == GET)
            strcpy_s(m, "GET");
        else
            strcpy_s(m, "POST");

        //http://msdn.microsoft.com/en-us/library/windows/desktop/aa385096%28v=vs.85%29.aspx
        HINTERNET internet = InternetOpenA(httpUseragent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
        if (internet != NULL)
        {
            //http://msdn.microsoft.com/en-us/library/windows/desktop/aa384363%28v=vs.85%29.aspx
            HINTERNET connect = InternetConnectA(internet, Host, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
            if (connect != NULL)
            {
                //http://msdn.microsoft.com/en-us/library/windows/desktop/aa384233%28v=vs.85%29.aspx
                HINTERNET request = HttpOpenRequestA(connect, m, url, "HTTP/1.1", NULL, NULL,
                    INTERNET_FLAG_HYPERLINK | INTERNET_FLAG_IGNORE_CERT_CN_INVALID |
                    INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
                    INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
                    INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS |
                    INTERNET_FLAG_NO_AUTH |
                    INTERNET_FLAG_NO_CACHE_WRITE |
                    INTERNET_FLAG_NO_UI |
                    INTERNET_FLAG_PRAGMA_NOCACHE |
                    INTERNET_FLAG_RELOAD, NULL);

                if (request != NULL)
                {
                    int datalen = 0;
                    if (data != NULL) datalen = strlen(data);
                    int headerlen = 0;
                    if (header != NULL) headerlen = strlen(header);

                    //http://msdn.microsoft.com/en-us/library/windows/desktop/aa384247%28v=vs.85%29.aspx
                    HttpSendRequestA(request, header, headerlen, data, datalen);

                    //http://msdn.microsoft.com/en-us/library/windows/desktop/aa384350%28v=vs.85%29.aspx
                    InternetCloseHandle(request);
                }
            }
            InternetCloseHandle(connect);
        }
        InternetCloseHandle(internet);
    }
    catch (...) {}
}

int main()
{
    char URL[1024];
    char* geturi = ""; // string.empty ( I want to make the request to the main page )
    wsprintfA(URL, geturi, NULL, NULL);
    Request(GET, "google.com", URL, NULL, NULL);
    // How can I see if it works? I want to get the webpage html source
    getchar(); // I avoided system("pause") function as it isn't portable.
}