2011-02-11 65 views
2

嘿所有我想登录到我的作品与WinInet的网页,这是我当前的代码:C++ wininet,连接到weblogin,如何设置cookie?

int main() 
{ 
    HINTERNET hInet = InternetOpenA("UserAgent/1.0", INTERNET_OPEN_TYPE_PRECONFIG,0, 0, 0); 
    if(!hInet) 
    { 
     printf("hInet Failed!\n"); 
     return -1; 
    } 

    HINTERNET hConnection = InternetConnectA(hInet,"app.tamigo.com",INTERNET_DEFAULT_HTTPS_PORT,"","", INTERNET_SERVICE_HTTP,0,0); 
    if (!hConnection) 
    { 
     InternetCloseHandle(hInet); 
     printf("InternetConnectA failed!\n"); 
     return -1; 
    } 

    HINTERNET hRequest = HttpOpenRequestA(hConnection, "Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",NULL,"https://app.tamigo.com/Home/Pages/Login.aspx", NULL, INTERNET_FLAG_KEEP_CONNECTION, 0); 
    if (!hRequest) 
    { 
     printf("BuildRequestHeader failed %d!\n",GetLastError()); 
     InternetCloseHandle(hConnection); 
     InternetCloseHandle(hInet); 
     return -1; 
    } 

    HttpSendRequestA(hRequest, NULL, 0, NULL, 0); 

    DWORD dwInfoLevel = HTTP_QUERY_RAW_HEADERS_CRLF; 
    DWORD dwInfoBufferLength = 10; 
    BYTE *pInfoBuffer = (BYTE *)malloc(dwInfoBufferLength+1); 
    while (!HttpQueryInfo(hRequest, dwInfoLevel, pInfoBuffer, &dwInfoBufferLength, NULL)) 
    { 
     DWORD dwError = GetLastError(); 
     if (dwError == ERROR_INSUFFICIENT_BUFFER) 
     { 
      free(pInfoBuffer); 
      pInfoBuffer = (BYTE *)malloc(dwInfoBufferLength+1); 
     } 
     else 
     { 
      fprintf(stderr, "HttpQueryInfo failed, error = %d (0x%x)\n", 
      GetLastError(), GetLastError()); 
      break; 
     } 
    } 
    pInfoBuffer[dwInfoBufferLength] = '\0'; 
    printf("%s", pInfoBuffer); 
    free(pInfoBuffer); 

    cin.get(); 
    return 1; 
} 

如果这个代码是正确的,我有我的用户名登录并通过,我使用了一个cookie“ Firefox插件篡改数据“。我怎样才能用wininet设置这个cookie?

非常感谢阅读和您的时间

+0

正如顺便说一句,如果你使用的是C++,没有理由使用malloc和free。使用新的和删除操作符。此外,在这种情况下,您根本不需要分配堆。在堆栈上创建缓冲区并将地址传递给HTTPQueryInfo。 – ThomasMcLeod 2011-02-11 19:17:36

回答

1

如果cookie已经从以前的WinInet请求存在,那么WinInet的将自动发送。但是,如果Cookie不存在于WinInet的Cookie缓存中(例如,如果您从其他来源获得cookie),则在致电HttpSendRequest()之前,您必须使用HttpAddRequestHeaders()来提供您自己的Cookie:请求标头。

0

只是添加到雷米Lebeau的答案(因为我没有足够的声誉评论)。

如果您不指定INTERNET_FLAG_NO_COOKIES标志为HttpOpenRequest,HttpAddRequestHeaders将忽略您设置“Cookie”标头的请求。请注意,这是禁用wininet的自动cookie管理,所以你需要照顾所有的cookie管理。

下面是详细信息(和另一种方式来做到这一点): https://groups.google.com/forum/#!topic/microsoft.public.inetsdk.programming.wininet/0O1hkyPCF-I