1

How do I post byte array picture using HttpWebRequest?

img is a byte array.

Below is the code, when I use the parse function, I cannot convert byte[] img to string :

// Parse is the function to replace @xxx with a new string
string docString = Parse("action=@action&img=@img&signloclat=@lat&lng=@lng&id=@id",
    "@action", "upload", "@img", img, "@lat", latitude, "@lng", longitude, "@id", id);
byte[] docByte = Encoding.ASCII.GetBytes(docString);

Uri wsHost = new Uri(EnpointAddress());
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(wsHost);

request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.ContentLength = docByte.Length;

Stream stream = request.GetRequestStream();
stream.Write(docByte, 0, docByte.Length);
stream.Close();

How can I post byte array with string using HttpWebRequest?

Can I cast the byte array to string before sending?

Thank you.

Alvin
  • 8,219
  • 25
  • 96
  • 177
  • 2
    usually, post image will use multi-part instead of post directly. depends on the site, it may need use cookie too since you may need to login the site first. more and more site may use JSON for post images. so, it really depends. – urlreader Oct 15 '12 at 03:09
  • the image is converted to byte array. – Alvin Oct 15 '12 at 06:51
  • http post can only post so much bytes. images usually is larger than that limit. so, you will need use multi-part in your post. See this example: http://stackoverflow.com/questions/219827/multipart-forms-from-c-sharp-client . * depends on the site you work on, it could be more complex. you need use http sniffer to check what it actually send. – urlreader Oct 16 '12 at 20:02

0 Answers0