What I'm looking for is how to send a file to a web service in Base64 format, using Http Post. I know this can be done using SOAP, but I'd like to know how to do this using Http Post.
My thinking was to use HttpWebRequest something along the lines of the code listed below.
protected Byte[] data;
// logic to stream the file to the byte array
// goes here
// post file to service
string postData = "file=" + HttpUtility.UrlEncode(data,0,filelength) +
"&name=" + HttpUtility.UrlEncode("image.jpg");
UTF8Encoding encoding = new UTF8Encoding();
byte[] buffer = encoding.GetBytes(postData);
string _webServiceURI="http://localhost/webservice.asmx";
string _webMethodName="Upload";
string uri = _webServiceURI + "/" + _webMethodName;
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
httpRequest.Method = "POST";
httpRequest.KeepAlive = false;
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = buffer.Length;
httpRequest.Timeout = 30000;
httpRequest.GetRequestStream().Write(buffer, 0, byte1.Length);
httpRequest.GetRequestStream().Close();
This is the stub for the web service :
[WebMethod (MessageName="Upload")]
public string Upload(Byte[] file, string name)
{
}
Joerg Jooss - 25 Mar 2005 11:35 GMT
> What I'm looking for is how to send a file to a web service in Base64
> format, using Http Post. I know this can be done using SOAP, but I'd
[quoted text clipped - 38 lines]
>
> }
You're doing some weird things here...
You cannot encode binary content using a character encoding. If you
want to use base64, why don't you use it, e.g. Convert.ToBase64String()?
Since you're not posting a web form, the content-type
application/x-www-form-urlencoded does not apply. Use
application/soap+xml, text/xml, or whatever the server-side accepts.
Cheers,

Signature
http://www.joergjooss.de
mailto:news-reply@joergjooss.de