如何通过chtmlview设置其他http标头? [英] How to set additional http headers via chtmlview?

查看:205
本文介绍了如何通过chtmlview设置其他http标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作自己的浏览器。它基于IE CHtmlView。我需要设置其他http标头。我搜索了很长时间,但仍然不知道。我该如何实现这个功能?

就像这样:

接受:* / *

test:test ==>这是我的额外标题

用户代理:Mozilla / 5.0(兼容; MSIE 10.0; xxx)==>我还需要修改用户代理。



我尝试了什么:



我试过功能

CHtmlView :: Navigate2(LPCTSTR lpszURL,DWORD dwFlags,LPCTSTR lpszTargetFrameName,LPCTSTR lpszHeaders,LPVOID lpvPostData,DWORD dwPostDataLen)

{

...

lpszHeaders = 测试:测试;

...

}

这将生效,但只是第一次导航到链接。如果导航源自浏览器(例如,在单击某个链接后),则调用不会通过Navigate2方法。那么还有其他一些地方,我可以在哪里修改所有请求标题,无论请求来自哪里?

如果使用CHtmlView无法做到这一点,我怎样才能达到预期的效果(添加一个请求http标头的几个键/值pais)?

I'm Making my own browser. It's based by IE CHtmlView. I need to set additional http headers. I searched for a long time, but still have no idea. How can I implement this function?
Just like this:
Accept: */*
test: test ==> this is my additonal header
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; xxx) ==>also I need to modify User-Agent.

What I have tried:

I tried in function
CHtmlView::Navigate2(LPCTSTR lpszURL, DWORD dwFlags,LPCTSTR lpszTargetFrameName,LPCTSTR lpszHeaders,LPVOID lpvPostData, DWORD dwPostDataLen)
{
...
lpszHeaders = "test:test";
...
}
This will take affect, but just the first time navigate to a link. if the navigation originates from within the browser (for example after clicking on some link), the call doesn't go through the Navigate2 methods. So is there some other place, where can I modify all the request headers, no matter where the request originated?
If this is not possible with the CHtmlView, how could I achieve the desired effect (adding a few key/value pais to the request http header)?

推荐答案

为什么要重写 CHtmlView :: Navigate2



根据文档 CHtmlView :: OnBeforeNavigate2 [ ^ ]应该用于处理导航事件,并且将被调用。



您还应该描述您尝试过的内容,而不是从其他问题中复制文本(修改CHtmlView http标头 [ ^ ])。



[更新]

可能的(未经测试的)实现可能如下所示:

Why you are overriding CHtmlView::Navigate2?

According to the documentation CHtmlView::OnBeforeNavigate2[^] should be used to handle navigation events and will be allways called.

You should also describe what you have tried rather than copying text from other questions (Modifying CHtmlView http headers[^]).

[UPDATE]
A possible (untested) implementation may look like:
void CMyHtmlView::OnBeforeNavigate2(LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName, CByteArray& baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel)
{
    // Check if X-MyHeader not set already
    if (_tcsstr(lpszHeaders, _T("X-MyHeader:")))
    {
        // Create our own header
        CString strHeaders(lpszHeaders);
        strHeaders.TrimRight();
        strHeaders.AppendFormat(_T("\nX-MyHeader: test\n"));
         // Set flags
        nFlags = navNoHistory | navNoReadFromCache | navNoWriteToCache;
        // Call again with modified header
        Navigate2(lpszURL, nFlags, baPostedData, lpszTargetFrameName, strHeaders);
        // Cancel this request
        *pbCancel = TRUE;
    }
    else
    {
        CHtmlView::OnBeforeNavigate2(lpszURL, nFlags, lpszTargetFrameName, baPostedData, lpszHeaders, pbCancel);
    }
}





[/更新]



[/UPDATE]


这篇关于如何通过chtmlview设置其他http标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆