title: How to upload image in Stream form to multipart rest service using Xamarin Form
body: <p>I was using this code but found that System.Net in xamarin does not having definitions of <strong>GetRequestStreamAsync</strong> and <strong>GetResponseAsync</strong>.</p>
  
  <p>So what is the alternate way of upload a file to server..</p>
  
  <pre><code>public static async Task&lt;string&gt; PostMultiPartForm(string url, byte[] file, string paramName, string contentType, Dictionary&lt;String, string&gt; nvc,
          string cookie)
      {
          // log.Debug(string.Format("Uploading {0} to {1}", file, url));
          string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
          byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
  
          HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
          wr.ContentType = "multipart/form-data; boundary=" + boundary;
          wr.Method = "POST";
          wr.Headers ["Cookie"] = cookie;
          //wr.KeepAlive = true;
          //wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
  
          Stream rs =  await wr.GetRequestStreamAsync();
  
          string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
          foreach (string key in nvc.Keys)
          {
              rs.Write(boundarybytes, 0, boundarybytes.Length);
              string formitem = string.Format(formdataTemplate, key, nvc[key]);
              byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
              rs.Write(formitembytes, 0, formitembytes.Length);
          }
          rs.Write(boundarybytes, 0, boundarybytes.Length);
  
          string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
          string header = string.Format(headerTemplate, paramName, file, contentType);
          byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
          rs.Write(headerbytes, 0, headerbytes.Length);
  
          rs.Write(file,0, file.Length);
  
  
          byte[] trailer = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
          rs.Write(trailer, 0, trailer.Length);
          //rs.Close();
          string responseString = String.Empty;
          WebResponse wresp = null;
          try
          {
              wresp = await wr.GetResponseAsync();
              Stream respStream = wresp.GetResponseStream();
              StreamReader respReader = new StreamReader(respStream);
              responseString = respReader.ReadToEnd();
              //log.Debug(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
          }
          catch (Exception ex)
          {
              //log.Error("Error uploading file", ex);
              if (wresp != null)
              {
                  //wresp.Close();
                  wresp = null;
              }
          }
          finally
          {
              wr = null;
          }
          return responseString;
  
      }
  </code></pre>