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); }
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); }
public void HttpPostWithFilesAndParameters3() { // this method sends a file and checks for the number of bytes recieved HttpClient client = new HttpClient(); string url = "http://www.fiddler2.com/sandbox/FileForm.asp"; HttpPost postMethod = new HttpPost(new Uri(url)); MultipartEntity multipartEntity = new MultipartEntity(); postMethod.Entity = multipartEntity; string fileName = "small-text.txt"; StringBody stringBody1 = new StringBody(Encoding.ASCII, "1", "1_"); multipartEntity.AddBody(stringBody1); FileInfo fi = ResourceManager.GetResourceFileInfo(fileName); FileBody fileBody1 = new FileBody("fileentry", fileName, fi, "text/plain"); multipartEntity.AddBody(fileBody1); StringBody stringBody2 = new StringBody(Encoding.ASCII, "_charset_", "windows-1252"); multipartEntity.AddBody(stringBody2); HttpResponse response = client.Execute(postMethod); Assert.AreEqual(200, response.ResponseCode); string responseString = EntityUtils.ToString(response.Entity); Assert.AreEqual(url, response.RequestUri.AbsoluteUri); Console.Write(responseString); }