views:

71

answers:

1

I'm trying to send a complex HTTP POST request to a web service. The web service was created using VS2008, in which you can set VS to create HTTP POST and GET interfaces alongside the SOAP one.

Now the request is asking for both string parameters (username, file description, etc.) and the file itself, represented as Base64Binary.

When creating Web Services in VS (not asp.net web services) and setting them to accept HTTP POST request - is it possible to send HTTP POST requests containing both string parameters and binary data?

A: 

You can get your bytes and convert to a base64 string by using Convert.ToBase64String() method. So, you'll get:

string base64 = Convert.ToBase64String(File.ReadAllBytes("yourfile.ext"));

If you're talking about how to send it, you can use HttpWebRequest , like this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("service.asmx");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (Stream post = request.GetRequestStream())
{
    string querystring =  // note you must encode that values
                "name=" + HttpUtility.UrlEncode(name) +
               "&desc=" + HttpUtility.UrlEncode(description) +
               "&data=" + HttpUtility.UrlEncode(base64);
    byte[] data = Encoding.UTF8.GetBytes(querystring);
    post.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());
Rubens Farias
I tried your code but I still get server error (500).I rephrased my question.