views:

33

answers:

2

Hi all,

I have Windows application, and I want to post data to an Url to get information from a webservice.
This is the code I use:

 private string PostData(string url, string postData)
 {
      HttpWebRequest request = null;
      if (m_type == PostTypeEnum.Post)
      {
           Uri uri = new Uri(url);
           request = (HttpWebRequest)WebRequest.Create(uri);
           request.Method = "POST";
           request.ContentType = "application/x-www-form-urlencoded";
           request.ContentLength = postData.Length;

           using (Stream writeStream = request.GetRequestStream())
           {
                UTF8Encoding encoding = new UTF8Encoding();
                byte[] bytes = encoding.GetBytes(postData);
                writeStream.Write(bytes, 0, bytes.Length);
           }
      }
      else
      {
           Uri uri = new Uri(url + "?" + postData);
           request = (HttpWebRequest)WebRequest.Create(uri);
           request.Method = "GET";
      }

      string result = string.Empty;
      using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
      {
           using (Stream responseStream = response.GetResponseStream())
           {
                using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                {
                     result = readStream.ReadToEnd();
                }
           }
      }
      return result;
 }

How can I add a file to the post data?
I need to send a file to a Page.

Any sample please?

Thanks in advanced, greetings

A: 

This post might help you.

Obalix
I want send file, and others data (input text, input checkbox, etc)... using WebClient and Upload not apply to my issue. Thanks anyway.
+1  A: 

You want to change your ContentType to "multipart/form-data" and then the body of the HTTP request becomes a series of MIME blocks with each block containing a different element of the data you're passing.

Here's the W3C specification for formatting the multipart/form-data.

David Smith
any sample code in C#, please ?? thanks !!!