zoukankan      html  css  js  c++  java
  • winhttp简单用法(3)post

    #include <string>
    #include <iostream>
    #include <windows.h>
    #include <winhttp.h>
    #pragma comment(lib,"winhttp.lib")
    #pragma comment(lib,"user32.lib")
    
    
    void main()
    {
    
        
        DWORD dwSize = 0;
        DWORD dwDownloaded = 0;
        LPSTR pszOutBuffer = NULL;
        HINTERNET  hSession = NULL,
                   hConnect = NULL,
                   hRequest = NULL;
    
        BOOL  bResults = FALSE;
    
        hSession=WinHttpOpen(L"User-Agent",WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0);
    
        if(hSession)
        {
            hConnect=WinHttpConnect(hSession,L"192.168.0.8",INTERNET_DEFAULT_HTTP_PORT,0);
        }
    
        if(hConnect)
        {
            hRequest=WinHttpOpenRequest(hConnect, L"POST",L"login.html",L"HTTP/1.1", WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES,0);
        }
        
        
        
        LPCWSTR header=L"Content-type: application/x-www-form-urlencoded/r/n";
    
        SIZE_T len = lstrlenW(header);
    
        WinHttpAddRequestHeaders(hRequest,header,DWORD(len), WINHTTP_ADDREQ_FLAG_ADD);
    
        if(hRequest)
        {
    
        std::string data="name=host&sign=xx11sad";
    
        const void *ss=(const char *)data.c_str();
    
        bResults=WinHttpSendRequest(hRequest, 0, 0,const_cast<void *>(ss),data.length(), data.length(), 0 );
    
            ////bResults=WinHttpSendRequest(hRequest,WINHTTP_NO_ADDITIONAL_HEADERS, 0,WINHTTP_NO_REQUEST_DATA, 0, 0, 0 );
        }
    
        if(bResults)
        {
            bResults=WinHttpReceiveResponse(hRequest,NULL);
    
        }
        
        if(bResults)
        {
            do
            {
                // Check for available data.
    
                 dwSize = 0;
    
                 if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
                 {
                     printf( "Error %u in WinHttpQueryDataAvailable.\n",GetLastError());
    
                     break;
                 }
    
                 if (!dwSize)
                     break;
    
                  pszOutBuffer = new char[dwSize+1];
    
                  if (!pszOutBuffer)
                  {
                       printf("Out of memory\n");
                    break;
                  }
    
                   ZeroMemory(pszOutBuffer, dwSize+1);
    
                   if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,  dwSize, &dwDownloaded))
                   {
                         printf( "Error %u in WinHttpReadData.\n", GetLastError());
                   }
                   else
                   {
                       printf("%s", pszOutBuffer);
                   }
    
                   delete [] pszOutBuffer;
    
                   if (!dwDownloaded)
                       break;
    
            } while (dwSize > 0);
        }
        
        
    
        if (hRequest) WinHttpCloseHandle(hRequest);
        if (hConnect) WinHttpCloseHandle(hConnect);
        if (hSession) WinHttpCloseHandle(hSession);
    
        
    
         system("pause");
    
    
    }
  • 相关阅读:
    Intent
    BroadcastReceiver
    AsyncTask两种线程池
    多线程、Service与IntentService的比较
    AsyncTask
    转:Android开发:使用DDMS Heap进行内存泄露调试
    (原创)Android Binder设计与实现
    (原)一句mpAudioPolicy->get_input引发的血案
    LOCAL_WHOLE_STATIC_LIBRARIES与LOCAL_STATIC_LIBRARIES的区别
    非static成员函数通过类名::来调用,空指针调用成员方法不出错!
  • 原文地址:https://www.cnblogs.com/ytjjyy/p/2484844.html
Copyright © 2011-2022 走看看