angle-v4.png

Making MultipartForm HttpClient request from Windows Phone 8.1

Hey Everyone, today  m going to tell you how to make a MultipartForm HttpClient  request from C# or in simple words you can say that “to send a multiple Post actions to a web in one httpclient request ”

 

so lets get started.

  • First of all make instance of HttpClient and set base address


HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://www.yourdomain.com");

  • Make MultiPartForm so that we can add all type of contents in it.

MultipartFormDataContent form = new MultipartFormDataContent();

  • Make HttpContetn because we need it to form different type of post contents i-e string,stream, byte arra

// for string type
HttpContent content  = new StringContent("your string type data you want to post");
form.Add(content, "name"); // name with which you will get its access on web (e-g: PHP)
// for image type
var stream = await file.OpenStreamForReadAsync(); // "file" is a StorageFile object of image
content = new StreamContent(stream);
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "image", // name with which you will get its access on web (e-g: PHP)
FileName = file.Name
};
form.Add(content);

  •  Now finally we are ready to make a HttpClient request

var response = await client.PostAsync("index.php", form);
// response will be shown using the following code
mytextblock.Text = response.Content.ReadAsStringAsync();

Thank you 🙂

#HappyCoding

Danyal MalikMaking MultipartForm HttpClient request from Windows Phone 8.1
Share this post

2 comments

Join the conversation
  • Draude - December 13, 2014 reply

    Please add the PHP script from the server too.

    Danyal Malik - December 21, 2014 reply

    Will upload soon.

Join the conversation

Related Posts