Re: Need Help on Http Header to add my own User-Agent with CDHTMLDialog
Sorry, I post again
==================================================================================
I use CDHTMLDialog to program a simple web browser, and I had a trobule
on Http Header modify.
I want add my own "User-Agent" when client use my application to browse
web.
//------------------------------------------------------------------------------------------------
#define WM_NVTO (WM_USER+1000)
class NvToParam
{
public:
CString URL;
DWORD Flags;
CString TargetFrameName;
CByteArray PostedData;
CString Headers;
DWORD dwPostDataLen;
};
//------------------------------------------------------------------------------------------------
LRESULT Ccdhtml_t1Dlg::OnNvTo(WPARAM wParam, LPARAM lParam)
{
NvToParam* pNvTo = (NvToParam*)wParam;
CDHtmlDialog::Navigate((LPCTSTR)pNvTo->URL, pNvTo->Flags,
(LPCTSTR)pNvTo->TargetFrameName, (LPCTSTR)pNvTo->Headers,
pNvTo->PostedData.GetData());
delete pNvTo;
return 1;
}
//------------------------------------------------------------------------------------------------
void Ccdhtml_t1Dlg::OnBeforeNavigate2(LPDISPATCH pDisp, VARIANT FAR*
URL, VARIANT FAR* Flags, VARIANT FAR* TargetFrameName, VARIANT FAR*
PostData, VARIANT FAR* Headers, BOOL FAR* Cancel)
{
CString strHeaders(V_BSTR(Headers));
// Check User-Agent
if(strHeaders.Find("User-Agent:LHPBrowser 1.0") < 0)
{
*Cancel = TRUE; // Cancel this request
if(!strHeaders.IsEmpty())
strHeaders += "\r\n";
// Add Customize Http Header
strHeaders += "User-Agent:LHPBrowser 1.0";
NvToParam* pNvTo = new NvToParam;
ASSERT(V_VT(URL) == VT_BSTR);
ASSERT(V_VT(TargetFrameName) == VT_BSTR);
ASSERT(V_VT(PostData) == (VT_VARIANT | VT_BYREF));
ASSERT(V_VT(Headers) == VT_BSTR);
ASSERT(Cancel != NULL);
USES_CONVERSION;
VARIANT* vtPostedData = V_VARIANTREF(PostData);
CByteArray array;
if (V_VT(vtPostedData) & VT_ARRAY)
{
// must be a vector of bytes
ASSERT(vtPostedData->parray->cDims == 1 &&
vtPostedData->parray->cbElements == 1);
vtPostedData->vt |= VT_UI1;
COleSafeArray safe(vtPostedData);
DWORD dwSize = safe.GetOneDimSize();
LPVOID pVoid;
safe.AccessData(&pVoid);
array.SetSize(dwSize);
LPBYTE lpByte = array.GetData();
memcpy(lpByte, pVoid, dwSize);
safe.UnaccessData();
}
// make real parameters out of the notification
CString strTargetFrameName(V_BSTR(TargetFrameName));
CString strURL(V_BSTR(URL));
DWORD nFlags = V_I4(Flags);
//=====================================================
pNvTo->URL = strURL;
pNvTo->Flags = nFlags;
pNvTo->TargetFrameName = strTargetFrameName;
pNvTo->PostedData.Copy(array);
pNvTo->Headers = strHeaders;
//=====================================================
// Post msg
PostMessage(WM_NVTO,(WPARAM)pNvTo);
return;
}
CDHtmlDialog::_OnBeforeNavigate2( pDisp, URL, Flags,
TargetFrameName, PostData, Headers, Cancel );
}
----------------------------------------------------------
After the testing, I found it will work fine on "http reqest", "http
form get" and "http form post".
But if my http form is "multipart/form-data" (upload file form), some
of the post datas will lost.
How should I fix it? Does any one can help me? Or any better method to
add http header when navigate a url?