public void HttpPostWithFilesAndParameters()
        {
            // this method sends an empty file (or no file :)
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri(Constants.HTTP_MULTIPART_POST_200));

            MultipartEntity multipartEntity = new MultipartEntity();
            StringBody stringBody1 = new StringBody(Encoding.ASCII, "param1", "value1");
            multipartEntity.AddBody(stringBody1);
            StringBody stringBody2 = new StringBody(Encoding.ASCII, "param2", "!#$^&*((<>");
            multipartEntity.AddBody(stringBody2);
            FileBody fileBody1 = new FileBody("photo", "me.file", null);
            multipartEntity.AddBody(fileBody1);
            postMethod.Entity = multipartEntity;
            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", "!#$^&*((<>"));
            md.Files.Add(new NameValuePair("me.file", "0"));
            Assert.AreEqual(md.ToString(), responseString);
            Assert.AreEqual(Constants.HTTP_MULTIPART_POST_200, response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }
示例#2
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);
        }
        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);
        }
示例#4
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 HttpPostWithFilesAndParameters2()
        {
            // this method sends a file and checks for the number of bytes recieved
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri(Constants.HTTP_MULTIPART_POST_200));

            MultipartEntity multipartEntity = new MultipartEntity();

            string fileName = "big-text.txt";

            FileInfo fi = ResourceManager.GetResourceFileInfo(fileName);
            FileBody fileBody1 = new FileBody("file", fileName, fi, "text/plain");

            multipartEntity.AddBody(fileBody1);
            postMethod.Entity = multipartEntity;

            StringBody stringBody1 = new StringBody(Encoding.ASCII, "param1", "value1");
            multipartEntity.AddBody(stringBody1);
            StringBody stringBody2 = new StringBody(Encoding.ASCII, "param2", "!#$^&*((<>");
            multipartEntity.AddBody(stringBody2);

            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", "!#$^&*((<>"));
            md.Files.Add(new NameValuePair(fileName, fi.Length.ToString()));
            Assert.AreEqual(md.ToString(), responseString);
            Assert.AreEqual(Constants.HTTP_MULTIPART_POST_200, response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }