public static void DoGetWithRedirects2()
        {
            HttpClient httpClient = new HttpClient();
            HttpGet httpGet = new HttpGet(new Uri("http://www.codescales.com/home"));

            HttpBehavior httpBehavior = new HttpBehavior();
            httpBehavior.AddStep(301, "http://www.codescales.com");
            httpBehavior.AddStep(200);

            HttpResponse httpResponse = httpClient.Execute(httpGet, httpBehavior);

            Console.WriteLine("Response Code: " + httpResponse.ResponseCode);
            Console.WriteLine("Response Code: " + httpResponse.Location);
            Console.WriteLine("Response Content: " + EntityUtils.ToString(httpResponse.Entity));
        }
        public void HttpGetWithRedirect3()
        {
            // this time with HTTPBehavior
            // make sure we were redirected and no exception is thrown
            HttpClient client = new HttpClient();
            HttpBehavior httpBehavior = new HttpBehavior();
            httpBehavior.AddStep(302, Constants.HTTP_REDIRECT_TARGET_1);
            httpBehavior.AddStep(200);
            HttpGet getMethod = new HttpGet(new Uri(Constants.HTTP_GET_302));
            HttpResponse response = client.Execute(getMethod, httpBehavior);

            Assert.AreEqual(200, response.ResponseCode);
            Assert.AreEqual(Constants.HTTP_REDIRECT_TARGET_1, response.RequestUri.AbsoluteUri);
            string responseString = EntityUtils.ToString(response.Entity);
            Console.Write(responseString);
        }
        public void HttpGetWithRedirect4()
        {
            // this time with HTTPBehavior
            // here both response code and location are wrong
            // make sure an exception is thrown

            HttpClient client = new HttpClient();
            HttpBehavior httpBehavior = new HttpBehavior();
            httpBehavior.AddStep(200);
            HttpGet getMethod = new HttpGet(new Uri(Constants.HTTP_GET_302));
            HttpResponse response = client.Execute(getMethod, httpBehavior);
        }
        public void HttpGetWithRedirect7()
        {
            // this time with HTTPBehavior
            // make sure we get only the first response
            // and do not make the second call
            HttpClient client = new HttpClient();
            HttpBehavior httpBehavior = new HttpBehavior();
            httpBehavior.AddStep(302, Constants.HTTP_REDIRECT_TARGET_1);
            HttpGet getMethod = new HttpGet(new Uri(Constants.HTTP_GET_302));
            HttpResponse response = client.Execute(getMethod, httpBehavior);

            Assert.AreEqual(302, response.ResponseCode);
            Assert.AreEqual(Constants.HTTP_REDIRECT_TARGET_1, response.Location);
        }
示例#5
0
 public HttpResponse Execute(HttpRequest request, HttpBehavior httpBehavior)
 {
     return Navigate(request, httpBehavior);
 }
示例#6
0
        private HttpResponse Navigate(HttpRequest request, HttpBehavior httpBehavior)
        {
            bool ContinueRedirect = true;
            HttpResponse response = null;

            HttpConnectionFactory connFactory = new HttpConnectionFactory();
            HttpConnection connection = connFactory.GetConnnection(request.Uri, this.m_proxy);

            HttpBehavior.RedirectStep rs = null;
            string redirectUri = null;
            int responseCode = 0;
            int redirectCounter = 0;

            try
            {
                while (ContinueRedirect)
                {
                    try
                    {
                        response = SendRequestAndGetResponse(connection, request);
                        redirectUri = response.Location;
                        responseCode = response.ResponseCode;

                        // response code 100 means that we need to wait for another response
                        // and receive the response from the socket again on the same connection
                        if (responseCode == 100)
                        {
                            response = GetResponse(connection);
                            redirectUri = response.Location;
                            responseCode = response.ResponseCode;
                        }

                        if (httpBehavior != null)
                        {
                            rs = httpBehavior.GetNextStep();
                            rs.Compare(responseCode, redirectUri);
                            ContinueRedirect = !httpBehavior.IsEmpty();
                        }
                        else
                        {
                            ContinueRedirect = (redirectCounter < this.m_maxRedirects && (responseCode == 301 || responseCode == 302));
                            redirectCounter++;
                        }

                        if (ContinueRedirect)
                        {
                            request = new HttpGet(new Uri(redirectUri));
                            // make sure the connection is still open and redirect url is from the same host
                            connection = connFactory.GetConnnection(request.Uri, this.m_proxy, connection);
                        }

                    }
                    catch (Exception ex)
                    {
                        int i = 0;
                        throw ex;
                    }

                }
            }
            finally
            {
                connection.Close();
            }
            return response;
        }
示例#7
0
        private HttpResponse Navigate(HttpRequest request, HttpBehavior httpBehavior)
        {
            bool         ContinueRedirect = true;
            HttpResponse response         = null;

            HttpConnectionFactory connFactory = new HttpConnectionFactory();
            HttpConnection        connection  = connFactory.GetConnnection(request.Uri, this.m_proxy);

            connection.Timeout = Timeout;

            HttpBehavior.RedirectStep rs = null;
            string redirectUri           = null;
            int    responseCode          = 0;
            int    redirectCounter       = 0;

            try {
                while (ContinueRedirect)
                {
                    try {
                        response     = SendRequestAndGetResponse(connection, request);
                        redirectUri  = response.Location;
                        responseCode = response.ResponseCode;

                        // response code 100 means that we need to wait for another response
                        // and receive the response from the socket again on the same connection
                        if (responseCode == 100)
                        {
                            response     = GetResponse(connection);
                            redirectUri  = response.Location;
                            responseCode = response.ResponseCode;
                        }

                        if (httpBehavior != null)
                        {
                            rs = httpBehavior.GetNextStep();
                            rs.Compare(responseCode, redirectUri);
                            ContinueRedirect = !httpBehavior.IsEmpty();
                        }
                        else
                        {
                            ContinueRedirect = (redirectCounter < this.m_maxRedirects && (responseCode == 301 || responseCode == 302));
                            redirectCounter++;
                        }

                        if (ContinueRedirect)
                        {
                            request = new HttpGet(new Uri(redirectUri));
                            // make sure the connection is still open and redirect url is from the same host
                            connection         = connFactory.GetConnnection(request.Uri, this.m_proxy, connection);
                            connection.Timeout = Timeout;
                        }
                    } catch (Exception ex) {
                        int i = 0;
                        throw ex;
                    }
                }
            } finally {
                connection.Close();
            }
            return(response);
        }
示例#8
0
 public HttpResponse Execute(HttpRequest request, HttpBehavior httpBehavior)
 {
     return(Navigate(request, httpBehavior));
 }