开发者

Add File to Post Data to Page

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 (St开发者_StackOverflow中文版ream 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 advance, greetings


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜