Example #1
0
        public static void DoPost()
        {
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri("http://www.w3schools.com/asp/demo_simpleform.asp"));

            List<NameValuePair> nameValuePairList = new List<NameValuePair>();
            nameValuePairList.Add(new NameValuePair("fname", "brian"));
            
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, Encoding.UTF8);
            postMethod.Entity = formEntity;
            
            HttpResponse response = client.Execute(postMethod);

            Console.WriteLine("Response Code: " + response.ResponseCode);
            Console.WriteLine("Response Content: " + EntityUtils.ToString(response.Entity));
        }
        public void HttpPost()
        {
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri(Constants.HTTP_POST_200));
            List<NameValuePair> nameValuePairList = new List<NameValuePair>();
            nameValuePairList.Add(new NameValuePair("param1","value1"));
            nameValuePairList.Add(new NameValuePair("param2","!#$^&*((<>"));
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, Encoding.UTF8);
            postMethod.Entity = formEntity;
            HttpResponse response = client.Execute(postMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            MessageData md = new MessageData();
            md.PostParameters.Add(new NameValuePair("param1", "value1"));
            md.PostParameters.Add(new NameValuePair("param2", "!#$^&*((<>"));
            Assert.AreEqual(md.ToString(), responseString);
            Assert.AreEqual(Constants.HTTP_POST_200, response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }
Example #3
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!"));

        }