public static GetResponseHeader ( Interop.WinHttp.SafeWinHttpHandle requestHandle, uint infoLevel, char &buffer, uint &index, string &headerValue ) : bool | ||
requestHandle | Interop.WinHttp.SafeWinHttpHandle | |
infoLevel | uint | |
buffer | char | |
index | uint | |
headerValue | string | |
return | bool |
public unsafe static bool GetResponseHeader(
SafeWinHttpHandle requestHandle,
uint infoLevel,
ref char[] buffer,
ref uint index,
out string headerValue)
{
const int StackLimit = 128;
Debug.Assert(buffer == null || (buffer != null && buffer.Length > StackLimit));
int bufferLength;
uint originalIndex = index;
if (buffer == null)
{
bufferLength = StackLimit;
char* pBuffer = stackalloc char[bufferLength];
if (QueryHeaders(requestHandle, infoLevel, pBuffer, ref bufferLength, ref index))
{
headerValue = new string(pBuffer, 0, bufferLength);
return true;
}
}
else
{
bufferLength = buffer.Length;
fixed (char* pBuffer = buffer)
{
if (QueryHeaders(requestHandle, infoLevel, pBuffer, ref bufferLength, ref index))
{
headerValue = new string(pBuffer, 0, bufferLength);
return true;
}
}
}
int lastError = Marshal.GetLastWin32Error();
if (lastError == Interop.WinHttp.ERROR_WINHTTP_HEADER_NOT_FOUND)
{
headerValue = null;
return false;
}
if (lastError == Interop.WinHttp.ERROR_INSUFFICIENT_BUFFER)
{
// WinHttpQueryHeaders may advance the index even when it fails due to insufficient buffer,
// so we set the index back to its original value so we can retry retrieving the same
// index again with a larger buffer.
index = originalIndex;
buffer = new char[bufferLength];
return GetResponseHeader(requestHandle, infoLevel, ref buffer, ref index, out headerValue);
}
throw WinHttpException.CreateExceptionUsingError(lastError);
}
WinHttpResponseParser::GetResponseHeader ( Interop.WinHttp.SafeWinHttpHandle requestHandle, uint infoLevel, char buffer ) : int |
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}"); } } } }