views:

2478

answers:

2

Hi guys 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.

Lets say my method is like this

[WebMethod]
public string TestMethod(string a)
{
    return a;
}

I call it like this using HttpRequest in C#

            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());
            }

This works perfectly fine. Now I have another webmethod that is defined like this

[WebMethod]
public string UploadFile(byte[] data)

I tried calling it like this

            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 that gives me a 500 internal error :(

Any help would be greatly appreciated.

A: 

You will probably need to base64 encode the binary data.

But the 500 error is a clue to look in the Windows event log and see what happened on the server side.

John Saunders
+1  A: 

Hi there,

You are using the HTTP POST/HTTP GET capability of the ASP.NET Web Service plumbing instead of sending an actual web-service call. This is a mechanism that allows you to test simple web services but it isn't really designed for use in a production application. In fact if you navigate to the web-service URL you'll find that it can't even display a test input form for that type of parameter. It might be possible to figure out a way to trick it into working, but to be honest, you should just use it the way it is intended and generate a web service proxy.

Within Visual Studio right mouse click on the project containing the client code and select Add Service or Web Reference. Then type in the URL to the web-service and it will generate a proxy. If you are using WCF it'll look something like this:

// ServiceNameClient is just a sample name, the actual name of your client will vary.
string data = "abc";
byte[] dataAsBytes = Encoding.UTF8.GetBytes(data);
ServiceNameClient client = new ServiceNameClient();
client.UploadFile(dataAsBytes);

Hope this helps.

Mitch Denny
lets say I was using C++ and HttpOpenRequest.. how would I do it then?
shergill
Not sure off the top of my head :)
Mitch Denny
If the above code works you could use Fiddler to see how the http request is constructed and then use that information to get your custom code to work.
Damien McGivern
http://www.fiddler2.com/fiddler2/
Damien McGivern