System.Net.Http.WinHttpResponseParser.GetResponseHeader C# (CSharp) Method

GetResponseHeader() private static method

Fills the buffer with the header value and returns the length, or returns 0 if the header isn't found.
private static GetResponseHeader ( Interop.WinHttp.SafeWinHttpHandle requestHandle, uint infoLevel, char buffer ) : int
requestHandle Interop.WinHttp.SafeWinHttpHandle
infoLevel uint
buffer char
return int
        private unsafe static int GetResponseHeader(SafeWinHttpHandle requestHandle, uint infoLevel, char[] buffer)
        {
            Debug.Assert(buffer != null, "buffer must not be null.");
            Debug.Assert(buffer.Length > 0, "buffer must not be empty.");

            int bufferLength = buffer.Length;
            uint index = 0;

            fixed (char* pBuffer = buffer)
            {
                if (!QueryHeaders(requestHandle, infoLevel, pBuffer, ref bufferLength, ref index))
                {
                    int lastError = Marshal.GetLastWin32Error();

                    if (lastError == Interop.WinHttp.ERROR_WINHTTP_HEADER_NOT_FOUND)
                    {
                        return 0;
                    }

                    Debug.Assert(lastError != Interop.WinHttp.ERROR_INSUFFICIENT_BUFFER, "buffer must be of sufficient size.");

                    throw WinHttpException.CreateExceptionUsingError(lastError);
                }
            }

            return bufferLength;
        }

Same methods

WinHttpResponseParser::GetResponseHeader ( Interop.WinHttp.SafeWinHttpHandle requestHandle, uint infoLevel, char &buffer, uint &index, string &headerValue ) : bool

Usage Example

        public static void AddResponseCookiesToContainer(WinHttpRequestState state)
        {
            HttpRequestMessage request         = state.RequestMessage;
            SafeWinHttpHandle  requestHandle   = state.RequestHandle;
            CookieContainer    cookieContainer = state.Handler.CookieContainer;

            Debug.Assert(state.Handler.CookieUsePolicy == CookieUsePolicy.UseSpecifiedCookieContainer);
            Debug.Assert(cookieContainer != null);

            // Get 'Set-Cookie' headers from response.
            char[] buffer = null;
            uint   index  = 0;
            string cookieHeader;

            while (WinHttpResponseParser.GetResponseHeader(
                       requestHandle, Interop.WinHttp.WINHTTP_QUERY_SET_COOKIE, ref buffer, ref index, out cookieHeader))
            {
                try
                {
                    cookieContainer.SetCookies(request.RequestUri, cookieHeader);
                    if (NetEventSource.Log.IsEnabled())
                    {
                        NetEventSource.Info(cookieContainer, $"Added cookie: {cookieHeader}");
                    }
                }
                catch (CookieException)
                {
                    // We ignore malformed cookies in the response.
                    if (NetEventSource.Log.IsEnabled())
                    {
                        NetEventSource.Error(cookieContainer, $"Ignoring invalid cookie: {cookieHeader}");
                    }
                }
            }
        }
All Usage Examples Of System.Net.Http.WinHttpResponseParser::GetResponseHeader