public static void DoGet()
        {
            HttpClient httpClient = new HttpClient();
            HttpGet httpGet = new HttpGet(new Uri("http://www.codescales.com"));
            HttpResponse httpResponse = httpClient.Execute(httpGet);

            Console.WriteLine("Response Code: " + httpResponse.ResponseCode);
            Console.WriteLine("Response Content: " + EntityUtils.ToString(httpResponse.Entity));
        }
        public static void DoGetWithProxy()
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Proxy = new Uri("http://localhost:8888/"); // default address of fiddler
            HttpGet httpGet = new HttpGet(new Uri("http://www.codescales.com"));
            HttpResponse httpResponse = httpClient.Execute(httpGet);

            Console.WriteLine("Response Code: " + httpResponse.ResponseCode);
            Console.WriteLine("Response Content: " + EntityUtils.ToString(httpResponse.Entity));
        }
        public static void DoGetWithRedirects()
        {
            HttpClient httpClient = new HttpClient();
            httpClient.MaxRedirects = 0;
            HttpGet httpGet = new HttpGet(new Uri("http://www.codescales.com/home"));
            HttpResponse httpResponse = httpClient.Execute(httpGet);

            Console.WriteLine("Response Code: " + httpResponse.ResponseCode);
            Console.WriteLine("Response Code: " + httpResponse.Location);
            Console.WriteLine("Response Content: " + EntityUtils.ToString(httpResponse.Entity));
        }
        public void HttpGet()
        {
            HttpClient client = new HttpClient();
            HttpGet getMethod = new HttpGet(new Uri(Constants.HTTP_GET_200));
            HttpResponse response = client.Execute(getMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            Assert.AreEqual(MessageData.Empty.ToString(), responseString);
            Assert.AreEqual(Constants.HTTP_GET_200, response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }
        public void HttpGetWithRedirect2()
        {
            // with SetMaxRedirects
            // make sure we were redirected
            HttpClient client = new HttpClient();
            client.MaxRedirects = 1;
            HttpGet getMethod = new HttpGet(new Uri(Constants.HTTP_GET_302));
            HttpResponse response = client.Execute(getMethod);

            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 HttpGetWithChunkedResponse()
        {
            // we use codescales.com that is hosted by google
            // the google server always uses chunked transfer-encoding

            HttpClient client = new HttpClient();
            HttpGet getMethod = new HttpGet(new Uri(Constants.HTTP_GET_200_CODESCALES_COM));
            HttpResponse response = client.Execute(getMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            Assert.AreEqual(Constants.HTTP_GET_200_CODESCALES_COM, response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }
        public void HttpGetWithChunkedResponse2()
        {
            // we use codescales.com that is hosted by google
            // the google server always uses chunked transfer-encoding

            HttpClient client = new HttpClient();
            HttpGet getMethod = new HttpGet(new Uri("http://forums.photographyreview.com/showthread.php?t=68825"));
            HttpResponse response = client.Execute(getMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            Assert.AreEqual("http://forums.photographyreview.com/showthread.php?t=68825", response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }
        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);
        }
示例#10
0
        public void HttpGetWithParameters()
        {
            HttpClient client = new HttpClient();
            HttpGet getMethod = new HttpGet(new Uri(Constants.HTTP_GET_200));
            getMethod.Parameters.Add("test", "1 and text");
            getMethod.Parameters.Add("test2", "2 and text <> &&");
            HttpResponse response = client.Execute(getMethod);

            Assert.AreEqual(200, response.ResponseCode);
            Assert.AreEqual(Constants.HTTP_GET_200, response.RequestUri.AbsoluteUri);
            // assert the parameters
            MessageData md = new MessageData();
            md.QueryParameters.Add(new NameValuePair("test", "1 and text"));
            md.QueryParameters.Add(new NameValuePair("test2", "2 and text <> &&"));
            string responseString = EntityUtils.ToString(response.Entity);
            Assert.AreEqual(md.ToString(), responseString);
            Console.Write(responseString);
        }
示例#11
0
        public void HttpPostWithParameters2()
        {
            HttpClient client = new HttpClient();

            // first request - to get cookies
            HttpGet getMethod = new HttpGet(new Uri(Constants.HTTP_POST_W3SCHOOLS));
            HttpResponse response = client.Execute(getMethod);
            Assert.AreEqual(200, response.ResponseCode);

            // second request - to send form data
            HttpPost postMethod = new HttpPost(new Uri(Constants.HTTP_POST_W3SCHOOLS));
            List<NameValuePair> nameValuePairList = new List<NameValuePair>();
            nameValuePairList.Add(new NameValuePair("fname", "user 1"));
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, Encoding.UTF8);
            postMethod.Entity = formEntity;
            response = client.Execute(postMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            Assert.IsTrue(responseString.Contains("Hello user 1!"));

        }
        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);
        }
示例#13
0
 /// <summary>
 /// Executes the command
 /// </summary>
 public void Execute()
 {
     HttpClient cli = new HttpClient();
     HttpGet get = new HttpGet(Uri);
     HttpResponse resp = cli.Execute(get);
 }
示例#14
0
        /// <summary>
        /// Gets the request.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="requestParamDic">The request parameter dic.</param>
        /// <returns></returns>
        public List<ResponseDataItem> getRequest(string url, Dictionary<string, string> requestParamDic)
        {
            httpGet = new HttpGet(new Uri(url));

            foreach (KeyValuePair<string, string> entry in requestParamDic)
            {
                httpGet.Parameters.Add(entry.Key, entry.Value);
            }
            //Get the response
            response = client.Execute(httpGet);
            //Get the response string
            string responseString = EntityUtils.ToString(response.Entity);
            //Get the Response Code and Save it
            string responseCode = response.ResponseCode.ToString();
            //Parse the response and return the values in the Dictionary
            Dictionary<String, String> responseCollection = null;
            if (responseCode == "200" && response != null)
            {
                //Console.WriteLine("Received the response" + responseString);
                responseCollection = new Dictionary<string, string>();
            }
            else
            {
                //Console.WriteLine("No Result returned");
            }
            return GetData(responseString);
        }
示例#15
0
        public void HttpGetWithCookies()
        {
            // with SetMaxRedirects
            // make sure we are not redirected
            HttpClient client = new HttpClient();
            client.MaxRedirects = 0;
            HttpGet getMethod = new HttpGet(new Uri(Constants.HTTP_GET_200_WITH_SET_COOKIES));
            getMethod.Parameters.Add("cookie1", "value1");
            getMethod.Parameters.Add("cookie2", "value2");
            HttpResponse response = client.Execute(getMethod);

            Assert.AreEqual(200, response.ResponseCode);
            Assert.AreEqual(Constants.HTTP_GET_200_WITH_SET_COOKIES, response.RequestUri.AbsoluteUri);

            getMethod = new HttpGet(new Uri(Constants.HTTP_GET_200));
            response = client.Execute(getMethod);

            Assert.AreEqual(200, response.ResponseCode);
            Assert.AreEqual(Constants.HTTP_GET_200, response.RequestUri.AbsoluteUri);

            // assert the cookies
            MessageData md = new MessageData();
            md.Cookies.Add(new NameValuePair("cookie1", "value1"));
            md.Cookies.Add(new NameValuePair("cookie2", "value2"));
            string responseString = EntityUtils.ToString(response.Entity);
            Assert.AreEqual(md.ToString(), responseString);
            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);
        }
示例#17
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;
        }