Exemplo n.º 1
0
        [Category("NotWorking")]          // bug #323388
        public void Async_GetResponse_Failure()
        {
            FileWebRequest req = (FileWebRequest)WebRequest.Create(_tempFileUri);

            req.Method        = "PUT";
            req.ContentLength = 1;
            req.ContentType   = "image/png";
            req.Timeout       = 500;

            IAsyncResult async = req.BeginGetRequestStream(null, null);

            try {
                req.GetResponse();
                Assert.Fail("#1");
            } catch (WebException) {
                // The operation has timed out
            }

            try {
                req.BeginGetResponse(null, null);
                Assert.Fail("#2");
            } catch (InvalidOperationException) {
                // Cannot re-call BeginGetRequestStream/BeginGetResponse while
                // a previous call is still in progress
            }

            using (Stream wstream = req.EndGetRequestStream(async)) {
                wstream.WriteByte(72);
            }

            // the temp file should not be in use
            Directory.Delete(_tempDirectory, true);
        }
Exemplo n.º 2
0
        [Category("NotWorking")]          // bug #323388
        public void Sync_GetResponse_Failure()
        {
            FileWebRequest req = (FileWebRequest)WebRequest.Create(_tempFileUri);

            req.Method        = "PUT";
            req.ContentLength = 1;
            req.ContentType   = "image/png";
            req.Timeout       = 500;

            using (Stream rs = req.GetRequestStream()) {
                try {
                    req.GetResponse();
                    Assert.Fail("#1");
                } catch (WebException) {
                    // The operation has timed out
                }

                try {
                    req.BeginGetResponse(null, null);
                    Assert.Fail("#2");
                } catch (InvalidOperationException) {
                    // Cannot re-call BeginGetRequestStream/BeginGetResponse while
                    // a previous call is still in progress
                }
            }

            // the temp file should not be in use
            _tempDirectory.Dispose();
        }
        /// <summary>
        /// Extends BeginGetResponse so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// filewebrequest.BeginGetResponse(callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginGetResponse(this FileWebRequest filewebrequest, AsyncCallback callback)
        {
            if (filewebrequest == null)
            {
                throw new ArgumentNullException("filewebrequest");
            }

            return(filewebrequest.BeginGetResponse(callback, null));
        }
        public void AsyncGetResponse()
        {
            FileWebRequest w = (FileWebRequest)WebRequest.Create(uri);

            message = "AsyncGetResponse";
            reset.Reset();
            IAsyncResult r = w.BeginGetResponse(new AsyncCallback(GetResponseCallback), w);

            Assert.IsNotNull(r, "IAsyncResult");
            if (!reset.WaitOne(timeout, true))
            {
                Assert.Ignore("Timeout");
            }
            Assert.IsNull(message, message);
        }
Exemplo n.º 5
0
        public void Async()
        {
            WebResponse res = null;

            try {
                FileWebRequest req = (FileWebRequest)WebRequest.Create(_tempFileUri);
                req.Method        = "PUT";
                req.ContentLength = 1;
                req.ContentType   = "image/png";

                req.Timeout = 2 * 1000;
                IAsyncResult async = req.BeginGetRequestStream(null, null);
                try {
                    req.BeginGetRequestStream(null, null);
                    Assert.Fail("#1 should've failed");
                } catch (InvalidOperationException) {
                    // Cannot re-call BeginGetRequestStream/BeginGetResponse while
                    // a previous call is still in progress
                }

                try {
                    req.GetRequestStream();
                    Assert.Fail("#3 should've failed");
                } catch (InvalidOperationException) {
                    // Cannot re-call BeginGetRequestStream/BeginGetResponse while
                    // a previous call is still in progress
                }

                using (Stream wstream = req.EndGetRequestStream(async)) {
                    Assert.IsFalse(wstream.CanRead, "#1r");
                    Assert.IsTrue(wstream.CanWrite, "#1w");
                    Assert.IsTrue(wstream.CanSeek, "#1s");

                    wstream.WriteByte(72);
                    wstream.WriteByte(101);
                    wstream.WriteByte(108);
                    wstream.WriteByte(108);
                    wstream.WriteByte(111);
                    wstream.Close();
                }

                Assert.AreEqual(1, req.ContentLength, "#1cl");
                Assert.AreEqual("image/png", req.ContentType, "#1ct");

                // stream written

                req = (FileWebRequest)WebRequest.Create(_tempFileUri);
                res = req.GetResponse();

                try {
                    req.BeginGetRequestStream(null, null);
                    Assert.Fail("#20: should've failed");
                } catch (InvalidOperationException) {
                    // Cannot send a content-body with this verb-type
                }

                try {
                    req.Method = "PUT";
                    req.BeginGetRequestStream(null, null);
                    Assert.Fail("#21: should've failed");
                } catch (InvalidOperationException) {
                    // This operation cannot be perfomed after the request has been submitted.
                }

                req.GetResponse();

                IAsyncResult async2 = req.BeginGetResponse(null, null);

                // this succeeds !!
                WebResponse res2 = req.EndGetResponse(async2);
                Assert.AreSame(res, res2, "#23");

                Assert.AreEqual(5, res.ContentLength, "#2 len");
                Assert.AreEqual("application/octet-stream", res.ContentType, "#2 type");
                Assert.AreEqual("file", res.ResponseUri.Scheme, "#2 scheme");

                Stream rstream = res.GetResponseStream();
                Assert.IsTrue(rstream.CanRead, "#3r");
                Assert.IsFalse(rstream.CanWrite, "#3w");
                Assert.IsTrue(rstream.CanSeek, "#3s");

                Assert.AreEqual(72, rstream.ReadByte(), "#4a");
                Assert.AreEqual(101, rstream.ReadByte(), "#4b");
                Assert.AreEqual(108, rstream.ReadByte(), "#4c");
                Assert.AreEqual(108, rstream.ReadByte(), "#4d");
                Assert.AreEqual(111, rstream.ReadByte(), "#4e");

                rstream.Close();

                try {
                    long len = res.ContentLength;
                    Assert.AreEqual((long)5, len, "#5");
                } catch (ObjectDisposedException) {
                    Assert.Fail("#disposed contentlength");
                }
                try {
                    WebHeaderCollection w = res.Headers;
                } catch (ObjectDisposedException) {
                    Assert.Fail("#disposed headers");
                }
                try {
                    res.Close();
                } catch (ObjectDisposedException) {
                    Assert.Fail("#disposed close");
                }
            } finally {
                if (res != null)
                {
                    res.Close();
                }
            }
        }