WinHTTP如何处理Cookie

WinHTTP如何处理CookieCookieHandlinginWinHTTPHTTPsessiondataispassedbetweentheclientandserverinthecookieheaderoftherequestortheresponse.TheserversendscookiestotheclientintheSet-cookiehe

大家好,又见面了,我是你们的朋友全栈君。

WinHTTP如何处理Cookie

HTTP session data is passed between the client and server in the cookie header of the request or the response. The server sends cookies to the client in the Set-cookie header of the response and the WinHTTP API resends the server cookie to the server in the cookie header of the request. The cookie handling specifications, described in rfc 2109 (HTTP State Management Mechanism), are implemented by default in WinHTTP. Recent cookie handling specifications, described in rfc 2964, are not supported by WinHTTP.

客户端和服务器之间的HTTP会话数据都带有Cookie数据。服务器通过Set-cookie头发送Cookie给客户端,WinHTTP客户端再把这些Cookie反发给服务器。WinHTTP实现了RFC2109(HTTP 状态管理机制)。而近期才有的rfc 2964 Cookie管理机制没有在WinHTTP中实现。

WinHTTP obtains the cookie from the servers Set-Cookie header and stores it in a cache on a per-session basis. This cookie is resent on subsequent requests in the same WinHTTP session where the target matches the source of the cookie. The WinHTTP API regenerates the request cookie header for each leg in the request.

WinHTTP从服务器发送的Set-Cookie头中获取Cookie并把它缓存起来是会话的基础。这个Cookie会在后续的请求中重新发送,会话也就是根据源地址和Cookie来界定的。WinHTTP API回味每次请求生成cookie头部。

The follow list describes several options that WinHTTP client applications can use to handle cookies:

下面是WinHTTP客户程序可以做的几种Cookie处理方法。

  • Automatic Cookie Handling – WinHTTP automatically handles cookies and the client application performs no custom cookie handling.
  • 自动处理 – WinHTTP自动处理Cookie,对cookie客户程序什么也不做。
  • Disable Automatic Cookie Handling – Automatic cookie handling in the WinHTTP API is disabled and no cookies are sent.
  • 关闭自动处理 – 关闭自动cookie处理后发送时就没有cookie啦。
  • Manually specify all Cookies – Automatic cookie handling is disabled and the client application adds or removes all cookie headers for each request in the session.
  • 手动设定 – 自动cookie处理关闭后,客户程序可以针对每个会话添加或删除各种cookie头部。
  • Manual and Automatic Cookie Handling – Combine automatic and manual cookie handling.
  • 手动设定和自动处理相结合 – 结合自动和手动处理

Disabling Automatic Cookie Handling

关闭自动处理

To disable cookie handling, the WinHTTP client application calls the WinHttpSetOption function with the dwOption parameter set to WINHTTP_OPTION_DISABLE_FEATURE, and thelpBuffer parameter set to WINHTTP_DISABLE_COOKIES. ThehInternet parameter can be either a session handle or a request handle. When cookie handling is disabled on a request handle that has sent a previous request, the client should manually remove existing request cookie headers with theWinHttpAddRequestHeaders function before sending the next request. For more information, seeRemoving Cookie Headers.

要关闭自动处理,WinHTTP客户程序需要调用WinHttpSetOption函数,把dwOption 参数设为WINHTTP_OPTION_DISABLE_FEATURE, thelpBuffer 参数设置为WINHTTP_DISABLE_COOKIES。hInternet参数可以是会话句柄也可以是请求句柄。

Note  The client application must set all cookies on the session after automatic mode has been disabled.

注意 如果自动模式被禁用,客户程序就必须在会话的后续过程中设置所有所需的cookie.

Manually Specifying All Cookies

手动设定

When automatic cookie handling is disabled, the WinHTTP client application has the option to manually specify all cookies. To manually set the cookie, the application callsWinHttpAddRequestHeaders specifying the cookie header in thepwszHeaders parameter. The client application should clear all cookie headers before resending the request.

当自动处理禁用后,WinHTTP客户程序有手动设置所有cookie的选择权。要手动设置cookie,程序要调用WinHttpAddRequestHeaders函数,通过设定pwszHeaders设定cookie头部。客户程序应该在重新发送请求前清除所有cookie。

The client application should also change the cookie header when the request has been redirected. To change the cookie on redirected requests, the client specifies a callback function withWinHttpSetStatusCallback that responds to the redirect callback case. The callback handler should clear the cookie previously sent on the request by callingWinHttpAddRequestHeaders. For more information about removing cookie headers, seeRemoving Cookie Headers.

客户程序也可以在请求被重定向后改变cookie头部。要这么做,客户端要利用WinHttpSetStatusCallback函数设定一个回调函数。回调函数首先通过调用WinHttpAddRequestHeaders清除掉所有跳转前的cookie。详情参见Removing Cookie Headers

Manual and Automatic Cookie Handling

手动自动结合

WinHTTP client applications can combine the WinHTTP automatic cookie handling mechanism with manual cookie handling. The application adds custom cookies to the automatically generated cookie header before sending the request with theWinHttpSendRequest function. The custom cookie should be the first cookie header in the request for the WinHTTP API to properly cache the cookie. The client application should also remove cookies sent on previous requests before resending a request on the same request handle. For more information, see Removing Cookie Headers.

WinHTTP客户程序可以把自动处理和手动处理结合起来。在调用WinHttpSendRequest函数发送前,程序可以把定制的cookie添加到自动生成的cookie头部。客户cookie就变成第一个WinHTTP API 请求和缓存时的cookie。客户程序也要在重发数据前清除已经发送过请求数据的句柄上的cookie。

Cookies added to a request before the call to WinHttpSendRequest are included in all WinHTTP requests sent on behalf of the nextWinHttpSendRequest andWinHttpReceiveResponse calls. The client application may need to clear the cookie header when the request has been redirected. To clear the cookie on redirected requests, the client specifies a callback function withWinHttpSetStatusCallback that responds to the redirect callback case. The callback handler should clear the cookie that was previously sent on the request by callingWinHttpAddRequestHeaders. The callback function may not set new custom cookies on the redirect callback. The client must wait forWinHttpReceiveResponse to complete before adding new cookies for the nextWinHttpSendRequest call.

在调用WinHttpSendRequest前cookie已经被添加进去了,这些请求会在后续的WinHttpSendRequest 和WinHttpReceiveResponse 发出。当请求被重定向后客户端也要清除cookie头部。要清除重定向后的请求中的cookie,客户端通过WinHttpSetStatusCallback函数指定回调函数来应对重定向这种情况。回调 函数应该清除之前请求中由WinHttpAddRequestHeaders设定的cookie回调函数也可以不设置新的客户cookie。客户端必须等待WinHttpReceiveResponse结束后才能添加新的cookie,然后再通过WinHttpSendRequest发送。

Removing Cookie Headers。

删除Cookie头部

The WinHTTP client application may need to clear the existing request cookie before resending a request to prevent cookies that were sent on previous requests from being sent again on the current request; for more information, see the following Note. Also be aware that Cookies do not have to be cleared before the first request is sent on the request handle. The client can clear existing cookies by callingWinHttpAddRequestHeaders with an empty cookie header in the pwszHeaders parameter and the WINHTTP_ADDREQ_FLAG_REPLACE flag set in thedwModifier parameter. The following code example shows how to clear the cookie header on the request.

WinHTTP客户端有时需要在重新发送请求前清除现有请求cookie,这样可以旧的cookie再次被发送;详情参加后续的注意事项。另外值得注意的是Cookie也不是非要在第一个请求通过请求句柄发送前被清除。客户端可以通过调用WinHttpAddRequestHeaders清除现有cookie,只要把代表cookie头部信息的pwszHeaders参数设为空,把dwModifier参数设为WINHTTP_ADDREQ_FLAG_REPLACE就可以了。下面代码展示了如何清除请求中的cooke头部。

WinHttpAddRequestHeaders( hRequest, 
                          L"Cookie:", 
                          -1, 
                          WINHTTP_ADDREQ_FLAG_REPLACE);

The WinHTTP API has different cookie handling behaviors for versions of the operating system earlier than Windows XP with Service Pack 2 (SP2) and Windows Server 2003 with Service Pack 1 (SP1).

在Windows XP SP2以前和Windows 2003 Sp1之前的操作系统中,WinHTTP API有不同的cookie处理方式。

Windows XP SP2 和之后版本以及 Windows Server 2003 SP1 和以后版本: 

The WinHTTP API clears all cookies sent on previous requests for the request handle. The client can manually add new cookie headers before each call to WinHttpSendRequest. If the automatic cookie handling functionality of the WinHTTP API has not been disabled, the WinHTTP API will append the new cookie header (or add a new cookie header if the client application did not add one manually) with the cookie from the server.

WinHTTP API清除请求句柄上所有之前发送请求中cookie。客户端可以在每次调用WinHttpSendRequest前手动添加新的cookie头部。如果自动模式没被禁用,WinHTTP API将会自己添加一个新的从服务器获取的cookie头部(或在客户程序没有手动添加时添加一个新的cookie头部)

Windows XP with SP2 and Windows Server 2003 with SP1:  

The WinHTTP API does not clear the request cookie header after WinHttpReceiveResponse completes. Cookies sent in previous requests will be resent in subsequent calls toWinHttpSendRequest. The WinHTTP client application should clear existing cookies headers before resending a request on the request handle.

在调用WinHttpReceiveResponse完成后,WinHTTP API 不会清除cookie头部。在后续调用WinHttpSendRequest重新发送之前请求中发送过的Cookie。在请求句柄上重新发送请求前,WinHTTP客户端程序应先清除现有cookie头部。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/157500.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • matlab矩阵怎么进行相加,matlab矩阵如何运算相加

    matlab矩阵怎么进行相加,matlab矩阵如何运算相加matlab矩阵如何运算相加发布时间:2020-12-1114:26:52来源:亿速云阅读:61作者:小新这篇文章主要介绍了matlab矩阵如何运算相加,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。matlab矩阵运算相加的方法:首先需要在matlab命令窗口中创建一个矩阵或数组;然后两矩阵相加需维数一致,在MATLAB中输入代码【A…

    2022年6月25日
    99
  • nand flash基础——基本结构[通俗易懂]

    nand flash基础——基本结构[通俗易懂]Array在String中,cell是串行方式连接的,一般32或64个一组,两端分别通过MSL连接到sourceline,MDL连接到bitline,并分别由晶体管控制开断。每个string和相邻的string(图中是上下方向)共用bitline。controlgate是通过wordlines连接在一起。Page是一个逻辑上的概念,page是由同一根wordline上的cel…

    2022年10月9日
    2
  • linux命令

    linux命令

    2021年5月31日
    103
  • django_session表_django queryset合并

    django_session表_django queryset合并前言session:session和cookie的作用有点类似,都是为了存储用户相关的信息。不同的是,cookie是存储在本地浏览器,session是一个思路、一个概念、一个服务器存储授权信息的解

    2022年7月30日
    6
  • 图解Linux命令之–dos2unix命令

    图解Linux命令之–dos2unix命令dos2unix命令–>DostoUNIX的缩写,用来将DOS格式的文本文件转换为UNIX格式find.-typef-execdos2unix{}\;find.-typef|xargs|dos2unix备注:1)为什么需要用dos2unix进行格式转换在Linux系统下经常会出现下列提示:/bin/sh^M:badinter…

    2022年7月14日
    20
  • matlab中@的用法[通俗易懂]

    matlab中@的用法[通俗易懂]@是用于定义函数句柄的操作符。函数句柄既是一种变量,可以用于传参和赋值;也是可以当做函数名一样使用。举例:sin是matlab中的一个函数,但sin只是函数名,还不是函数句柄,不可以用于传参。f=@sin;这行代码定义了一个函数句柄,变量名是f。这样就可以当做参数传递了(这就是上面代码中的意义所在),而且还可以跟sin函数按相同的语法规则使用:g=f;%g也是函数句柄,其“值”和f一样…

    2022年7月17日
    15

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号