I'm tr开发者_如何学运维ying 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?
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());
精彩评论