HttpWebRequest POST字节数组

时间:2012-10-15 02:48:24

标签: c# .net

如何使用HttpWebRequest发布字节数组图片?

img是一个字节数组。

下面是代码,当我使用parse函数时,我无法将byte [] img转换为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();

如何使用HttpWebRequest发布带字符串的字节数组?

我可以在发送之前将字节数组转换为字符串吗?

谢谢。

0 个答案:

没有答案