CEF3写Cookie的示例代码段

//测试写cookie
void CtestCEF2Dlg::OnBnClickedBtnWriteCookie()
{
	if (m_cefBrowser&&m_cefBrowser->IsLoading() == false)
	{
		//http://www.magpcss.net/test/cookies.php
		//http://kagula-work-pc:8080/TestCookie/FirstServlet
		//http://kagula.com:8080/TestCookie/FirstServlet
		CString cstrURL;
		GetDlgItem(IDC_EDIT_URL)->GetWindowText(cstrURL);
		m_cefBrowser->GetMainFrame()->LoadURL(cstrURL.GetBuffer());

		/*
		老版本的CEF3有下面这个问题:
		即只能用kagula.com,域名方式访问
		不能用localhost或则127.0.0.1或则172.16.18.80等形式访问的问题
		*/
		cstrURL.Replace(L'\\', L'/');
		if (cstrURL.Find(L"http://")==0)
		{
			cstrURL = cstrURL.Mid(7, cstrURL.GetLength() - 7);
		}
		int nPos = cstrURL.Find(L"/");
		if (nPos>0)
		{
			cstrURL = cstrURL.Left(nPos);
		}

		kagula::SetCookie(cstrURL.GetBuffer(), L"KagulaCookieKey", L"KagulaCookieValue");//kagula.com//www.magpcss.net
	}
}


#include "CEF3Helper.h"
#include "../include/cef_app.h"
#include "../include/cef_browser.h"
#include "../include/cef_frame.h"
#include "../include/cef_sandbox_win.h"
#include "../include/cef_task.h"
#include "../include/cef_runnable.h"
namespace kagula
{
	void SetCookie(std::wstring domain, std::wstring key, std::wstring value)
	{
		/*
		测试写cookie
		参考资料
		[1]如何设置本地域名
		http://stackoverflow.com/questions/1134290/cookies-on-localhost-with-explicit-domain
		[2]测试cookie的网站
		http://www.magpcss.net/test/cookies.php
		*/
		CefRefPtr<CefCookieManager> manager = CefCookieManager::GetGlobalManager();
		CefCookie cookie;
		CefString(&cookie.name).FromWString(key.c_str());
		CefString(&cookie.value).FromWString(value.c_str());
		CefString(&cookie.domain).FromWString(domain.c_str());//www.magpcss.net//kagula-work-pc
		CefString(&cookie.path).FromASCII("/");
		cookie.has_expires = true;
		//cookie.expires.year = 2200;
		//cookie.expires.month = 4;
		//cookie.expires.day_of_week = 5;
		//cookie.expires.day_of_month = 11;

		std::wstring httpDomain = L"http://";
		httpDomain.append(domain);
		CefPostTask(TID_IO, NewCefRunnableMethod(manager.get(), &CefCookieManager::SetCookie,
			CefString(httpDomain.c_str()), cookie));//http://www.magpcss.net//kagula-work-pc
	}
}


你可能感兴趣的:(CEF3写Cookie的示例代码段)