示例#1
0
        void InitializeNativeRequest()
        {
            native = NativeMethods.deployment_create_http_request(System.Windows.Deployment.Current.native, HttpRequestOptions.CustomHeaders);
            if (native == IntPtr.Zero)
            {
                throw new NotSupportedException("Failed to create unmanaged HttpRequest object");
            }

            Events.AddHandler(native, EventIds.HttpRequest_StartedEvent, started);
            Events.AddHandler(native, EventIds.HttpRequest_WriteEvent, available);
            Events.AddHandler(native, EventIds.HttpRequest_StoppedEvent, finished);

            var nativeUri = UriHelper.ToNativeUri(RequestUri);

            NativeMethods.http_request_open(native, Method, nativeUri, DownloaderAccessPolicy.NoPolicy);
            NativeMethods.uri_free(nativeUri);

            long request_length = 0;

            byte[] body = null;
            if (request != null)
            {
                request.Close();
                body           = request.GetData();
                request_length = body.Length;
            }

            if (request_length > 1)
            {
                // this header cannot be set directly inside the collection (hence the helper)
                NativeMethods.http_request_set_header(native, "content-length", request_length.ToString(), false);
            }

            foreach (string header in Headers)
            {
                NativeMethods.http_request_set_header(native, header, Headers [header], false);
            }

            if (request_length > 1)
            {
                NativeMethods.http_request_set_body(native, body, body.Length);
            }

            NativeMethods.http_request_send(native);
        }