title: How to use HttpWebRequest to call a Web Service Operation that takes in a byte[] parameter?
body: <p>I am trying to call a [webmethod] from C#. I can call simple webmethod that take in 'string' parameters. But I have a webmethod that takes in a 'byte[]' parameter. I am running into '500 internal server error' when I try to call it. Here is some example of what I am doing.</p>
  
  <p>Lets say my method is like this</p>
  
  <pre><code>[WebMethod]
  public string TestMethod(string a)
  {
      return a;
  }
  </code></pre>
  
  <p>I call it like this using HttpRequest in C#</p>
  
  <pre><code>            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
              req.Credentials = CredentialCache.DefaultCredentials;
              req.Method = "POST";
              // Set the content type of the data being posted.
              req.ContentType = "application/x-www-form-urlencoded";
  
              string inputData = "sample webservice";
              string postData = "a=" + inputData;
              ASCIIEncoding encoding = new ASCIIEncoding();
              byte[] byte1 = encoding.GetBytes(postData);
  
              using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
              {
                  StreamReader sr = new StreamReader(res.GetResponseStream());
                  string txtOutput = sr.ReadToEnd();
                  Console.WriteLine(sr.ReadToEnd());
              }
  </code></pre>
  
  <p>This works perfectly fine. Now I have another webmethod that is defined like this</p>
  
  <pre><code>[WebMethod]
  public string UploadFile(byte[] data)
  </code></pre>
  
  <p>I tried calling it like this</p>
  
  <pre><code>            ASCIIEncoding encoding = new ASCIIEncoding();
              string postData = "data=abc";
              byte[] sendBytes = encoding.GetBytes(postData);
              req.ContentLength = sendBytes.Length;
              Stream newStream = req.GetRequestStream();
              newStream.Write(sendBytes, 0, sendBytes.Length);
  </code></pre>
  
  <p>But that gives me a 500 internal error :(</p>