Exemplo n.º 1
0
 public async void TestGET()
 {
     var request = new HttpRequest(new Uri("http://www.bing.com"));
     var response = await request.GetResponseAsync().ConfigureAwait(false);
     Assert.AreEqual("http://cn.bing.com/", response.ResponseUri.AbsoluteUri);
     response.Close();
 }       
Exemplo n.º 2
0
        public async void TestProxyWithRedirect()
        {
            var proxy = new System.Net.WebProxy("127.0.0.1", 8580);
            proxy.BypassList = new string[] { "http://www.bing.com" };

            //use a proxy 
            var request = new HttpRequest(new Uri("http://www.google.com"))
            {               
                Proxy = proxy
            };
            var response = await request.GetResponseAsync().ConfigureAwait(false);
            Assert.AreEqual(200, (int)response.StatusCode);
            Assert.AreEqual("www.google.com", response.ResponseUri.Host);
            response.Close();

            //disable a proxy
           request = new HttpRequest(new Uri("http://www.bing.com"))
            {
                Proxy = proxy                
            };          
           response = await request.GetResponseAsync().ConfigureAwait(false);
            Assert.AreEqual(200, (int)response.StatusCode);
            Assert.AreEqual("cn.bing.com", response.ResponseUri.Host);
            response.Close();
        }
Exemplo n.º 3
0
 public async void TestHttp2Https()
 {            
     var request = new HttpRequest(new Uri("http://www.github.com"));            
     var response = await request.GetResponseAsync().ConfigureAwait(false);
     Assert.AreEqual(200, (int)response.StatusCode);
     Assert.AreEqual("https", response.ResponseUri.Scheme);
     Assert.AreEqual("https://github.com/", response.ResponseUri.AbsoluteUri);
 }
Exemplo n.º 4
0
 public async void TestProxy()
 {
     var proxy = new System.Net.WebProxy("127.0.0.1", 8580);
     var request = new HttpRequest(new Uri("http://www.google.com"))
     {
         Proxy = proxy
     };
     var response = await request.GetResponseAsync().ConfigureAwait(false);
     Assert.AreEqual(200, (int)response.StatusCode);
 }
Exemplo n.º 5
0
 public async void TestOptions()
 {
     var request = new HttpRequest(new Uri("http://www.bing.com"))
     {
         Method = HttpMethod.Options
     };
     var response = await request.GetResponseAsync().ConfigureAwait(false);
     Assert.AreEqual(405, (int)response.StatusCode);//405 Method Not Allowed
     Assert.AreEqual("GET,HEAD,TRACE", response.Headers["Allow"]);
     response.Close();
 }
Exemplo n.º 6
0
 public async void TestHEAD()
 {
     var request = new HttpRequest(new Uri("http://cn.bing.com"))
     {
         Method = HttpMethod.Head
     };
     var response = await request.GetResponseAsync().ConfigureAwait(false);
     Assert.AreEqual("http://cn.bing.com/", response.ResponseUri.AbsoluteUri);
     Assert.IsNull(response.GetResponseStream());
     response.Close();
 }
Exemplo n.º 7
0
 public async void TesDisableRedirect()
 {
     var request = new HttpRequest(new Uri("http://www.bing.com"))
     {
         AllowAutoRedirect = false
     };
     var response = await request.GetResponseAsync().ConfigureAwait(false);
     Assert.AreEqual("http://www.bing.com/", response.ResponseUri.AbsoluteUri);
     Assert.AreEqual(301, (int)response.StatusCode);
     response.Close();
 }
Exemplo n.º 8
0
 public async void TestPOST()
 {
     var request = new HttpRequest(new Uri("http://api.jquery.com/"))
     {
         Method = HttpMethod.Post
     };
     var post_stream = request.GetRequestStream();
     request.Headers.ContentType = "application/x-www-form-urlencoded";            
     var bytes = Encoding.UTF8.GetBytes("s=ajax");//search ajax keywords
     post_stream.Write(bytes, 0, bytes.Length);
     post_stream.Close();
     var response = await request.GetResponseAsync().ConfigureAwait(false);
     Assert.AreEqual(200,(int) response.StatusCode);
     using (var sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
     {               
         var index = sr.ReadToEnd().IndexOf("<span>ajax</span>");
         Assert.IsTrue(index > 0);
     }
     response.Close();
 }
Exemplo n.º 9
0
 public async void TestAutoDecompression()
 {
     var request = new HttpRequest(new Uri("http://www.bing.com"))
     {
         AutomaticDecompression = true
     };
     request.Headers.AcceptEncoding = "gzip,deflate";            
     var response = await request.GetResponseAsync().ConfigureAwait(false);
     Assert.IsNotNull(response.Headers.ContentEncoding);
     using (var sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
     {
         var html = sr.ReadToEnd();
         Console.WriteLine(html);
         sr.Close();
     }
     response.Close();
 }
Exemplo n.º 10
0
 public async void TestSmallBuffer()
 {
     var request = new HttpRequest(new Uri("http://cn.bing.com"));
     request.BuffSize = 15;
     var response = await request.GetResponseAsync().ConfigureAwait(false);
     Assert.AreEqual(200, (int)response.StatusCode);
 }
Exemplo n.º 11
0
 public async void TestHttps()
 {
     var request = new HttpRequest(new Uri("https://www.bing.com"));
     var response = await request.GetResponseAsync().ConfigureAwait(false);
     Assert.AreEqual(200, (int)response.StatusCode);
 }