Пример #1
0
        public WinHttpSession(
            Uri responseUri,
            SafeWinHttpHandle hConnect,
            SafeWinHttpHandle hRequest,
            Dictionary <string, List <string> >?headers,
            Dictionary <string, string>?cookies,
            byte[]?postData = null)
        {
            this.responseUri = responseUri;
            this.hConnect    = hConnect;
            this.hRequest    = hRequest;
            this.callback    = new(WinHttpCallback);
            IntPtr oldCallback = WinHttpSetStatusCallback(
                hRequest,
                this.callback,
                WINHTTP_CALLBACK_FLAG_REDIRECT,
                IntPtr.Zero);

            if (oldCallback == new IntPtr(WINHTTP_INVALID_STATUS_CALLBACK))
            {
                int lastError = Marshal.GetLastWin32Error();
                if (lastError != ERROR_INVALID_HANDLE) // Ignore error if handle was already closed.
                {
                    throw new IOException(nameof(WinHttpSetStatusCallback));
                }
            }
            this.headers = headers;
            this.cookies = cookies;
            if (postData != null && postData.Length > 0)
            {
                this.postData = Marshal.AllocHGlobal(postData.Length);
                Marshal.Copy(postData, 0, this.postData.Value, postData.Length);
                this.postDataSize = postData.Length;
            }
            this.responseStream = new WinHttpResponseStream(this.hRequest);
        }
Пример #2
0
 public static extern IntPtr WinHttpSetStatusCallback(
     SafeWinHttpHandle handle,
     WINHTTP_STATUS_CALLBACK callback,
     uint notificationFlags,
     IntPtr reserved);
Пример #3
0
 public static extern IntPtr WinHttpSetStatusCallback(
     SafeWinHttpHandle handle,
     WINHTTP_STATUS_CALLBACK callback,
     uint notificationFlags,
     IntPtr reserved);