I am trying to call [webmethod] from C #. I can name a simple web method that takes "string" parameters. But I have a web method that accepts the "byte []" parameter. When I try to call it, I run an "internal server error". Here is an example of what I am doing.
Let's say my method is like this
[WebMethod]
public string TestMethod(string a)
{
return a;
}
I call it that way using HttpRequest in C #
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Credentials = CredentialCache.DefaultCredentials;
req.Method = "POST";
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());
}
This works great. Now I have another web method that is defined as
[WebMethod]
public string UploadFile(byte[] data)
I tried calling him like that
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);
But this gives me 500 internal errors :(
source
share