C# HttpWebRequest POST字节数组

C# HttpWebRequest POST字节数组,c#,.net,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", "@acti

如何使用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发布带有字符串的字节数组

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


谢谢。

通常,post图像将使用多部分,而不是直接发布。根据站点的不同,可能还需要使用cookie,因为您可能需要先登录站点。越来越多的网站可能会使用JSON发布图片。所以,这要看情况而定。映像被转换为字节数组。http post只能发布这么多字节。图像通常大于该限制。所以,你需要在你的文章中使用多部分。请参见此示例:*取决于你工作的网站,它可能会更复杂。您需要使用http嗅探器来检查它实际发送的内容。