none
请问C语言怎么在打开某个网站并且发送post RRS feed

  • 问题

  • 请问C语言怎么打开某个网站的页面并且向他发送post数据

    在软件里面设置网址的函数是什么,发送post的是什么函数

    2012年11月10日 12:49

全部回复

  • c 语言有很多方法可以向目标发送 http post,取决于你使用什么库。下面使用 WinINet 库。
    #pragma comment(lib, "wininet.lib")
    
    #include <windows.h>
    #include <wininet.h>
    #include <stdio.h>
    
    HINTERNET g_hInternet, g_hConnect, g_hRequest;
    HANDLE g_hEvent;
    BOOL g_bAllDone = FALSE;
    
    VOID CALLBACK Callback(HINTERNET h, DWORD_PTR dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dw) {
        if (dwContext == 1) {
            if (dwInternetStatus == INTERNET_STATUS_HANDLE_CREATED) {
                g_hConnect = (HINTERNET)((INTERNET_ASYNC_RESULT*)lpvStatusInformation)->dwResult;
                SetEvent(g_hEvent);
            }
        } else {
            switch (dwInternetStatus) {
            case INTERNET_STATUS_HANDLE_CREATED:
                g_hRequest = (HINTERNET)((INTERNET_ASYNC_RESULT*)lpvStatusInformation)->dwResult;
                SetEvent(g_hEvent);
                break;
    
            case INTERNET_STATUS_REQUEST_COMPLETE:
                SetEvent(g_hEvent);
                break;
    
            case INTERNET_STATUS_RESPONSE_RECEIVED:
                if (*(DWORD*)lpvStatusInformation == 0)
                    g_bAllDone = TRUE;
                break;
            }
        }
        UNREFERENCED_PARAMETER(h);
        UNREFERENCED_PARAMETER(dw);
    }
    
    int main() {
        char const* szServer    = "social.msdn.microsoft.com";
        char const* szUrl       = "/Search/zh-CN/?query=123";
        char const* szVerb      = "POST";
    
        char chBuffer[256];
    
        g_hInternet = InternetOpen("Mozilla/4.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
        g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    
        InternetSetStatusCallback(g_hInternet, Callback);
        InternetConnect(g_hInternet, szServer, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
        WaitForSingleObject(g_hEvent, INFINITE);
    
        g_hRequest = HttpOpenRequest(g_hConnect, szVerb, szUrl, NULL, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE, 2);
        WaitForSingleObject(g_hEvent, INFINITE);
    
        HttpSendRequest(g_hRequest, NULL, 0, NULL, 0);
        WaitForSingleObject(g_hEvent, INFINITE);
    
        do {
            INTERNET_BUFFERS ib = { 0 };
    
            ib.dwStructSize = sizeof ib;
            ib.lpvBuffer = chBuffer;
            ib.dwBufferLength = sizeof chBuffer - 1;
    
            if (!InternetReadFileEx(g_hRequest, &ib, 0, 2))
                WaitForSingleObject(g_hEvent, INFINITE);
    
            chBuffer[ib.dwBufferLength] = 0;
            printf("%s", chBuffer);
    
            if (ib.dwBufferLength == 0) 
                g_bAllDone = TRUE;
        } while (!g_bAllDone);
    
        CloseHandle(g_hEvent);
        InternetCloseHandle(g_hRequest);
        InternetCloseHandle(g_hConnect);
        InternetCloseHandle(g_hInternet);
    }
    
    2012年11月12日 15:24
  • 这个不是C语言不C语言的问题

    需要调用Windows的API实现


    新浪微博http://weibo.com/xianglitian,欢迎围观

    2012年11月13日 15:26