CInternetSession :: SetCookie示例

时间:2014-10-09 23:27:28

标签: http cookies mfc wininet

CInternetSession :: SetCookie的MSDN文档推迟到InternetSetCookie Win32 message,但我不得不继续挖掘以找到Managing Cookies under WinINet中的示例。我认为使用它的示例代码缺乏,并且有用。

1 个答案:

答案 0 :(得分:0)

这就是我所拥有的:

CInternetSession session;
CHttpConnection* connection=0;
try {
  INTERNET_PORT port = 80;
  CString server("mydomain.com");
  CString path("/cookie-test.cgi");
  CString cookieUrl("http://"+server+path);
  BOOL res = session.SetCookie((LPCSTR)cookieUrl,0,"my_cookie=Set My Cookie!");
  if(res == FALSE) {
    DWORD error GetLastError();
    // code to format error message
  }
  connection = session.GetHttpConnection(server,port);
  file = connection->OpenRequest(
    CHttpConnection::HTTP_VERB_GET, //Action verb
    path //Object to perform action on
    ,0,1,0,0,INTERNET_FLAG_*); // pick your flags
  file->SendRequest();
  // Check status, read response, process
}
catch(CInternetException *e) {
  // report error
}
connection->Close();
file->Close();
delete file;
delete connection;

我的cookie-test.cgi看到my_cookie的值为"设置我的Cookie!"