C#上传图片400错误请求

时间:2016-09-04 10:50:14

标签: c# upload form-data bad-request boundary

我正在以编程方式尝试更改我的帐户的网站头像(ask.fm)。我模仿了浏览器发送的相同请求,但每次我调用它都会给我“400 Bad-request”。 我注意到的一些调试是边界如果改变会产生错误,所以可能是他们使用的是一些算法而不是随机数,否则我不明白为什么我的代码不起作用。 来自浏览器的原始请求:

POST http://upload5 .ask. fm/upload/api-avatar HTTP/1.1
Host: upload5 .ask. fm
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0
Accept: */*
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://ask. fm/account/settings/profile
Content-Length: 305167
Content-Type: multipart/form-data; boundary=---------------------------22412596920869
Origin: http://ask. fm
DNT: 1
Connection: keep-alive

-----------------------------22412596920869
Content-Disposition: form-data; name="file"; filename="1.png"
Content-Type: image/png

<...image bytes...>

-----------------------------22412596920869
Content-Disposition: form-data; name="specs"

U2FsdGVkX18XLeEmxsI+tytSpHcAV/UrBO8wmsoYERnL59rHGwF5Yz5QOeVl3GHap3ufLDGLyWxU4cCt28kPaSPq/iOusnVqRiYyp1nDD8VSvSLWa+Ndg8/TjKgaMqCgDNOrOSOPmxcV2kNPdDXNNvnuLWljYhPlBxrMGR2UeoqLNpJwsZCvs1UvVWcZFDy9SizVAOBDl6f1AIyqdvWiwjYGvg7jZ5q6ykTZda1pYuk=
-----------------------------22412596920869
Content-Disposition: form-data; name="ts"

1472985276554
-----------------------------22412596920869--

来自我的申请的原始请求:

POST http://upload5 .ask. fm/upload/api-avatar HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0
Accept: */*
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://ask. fm/account/settings/profile
Origin: http://ask. fm
DNT: 1
Content-Type: multipart/form-data; boundary=-----------------------------680184
Host: upload5 .ask. fm
Content-Length: 305133

-----------------------------680184
Content-Disposition: form-data; name="file"; filename="1.png"
Content-Type: image/png

<...image bytes...>

-----------------------------680184
Content-Disposition: form-data; name="specs"

U2FsdGVkX19t+38KPvBBtpXmUUu8QGk61dKTvb0hEGZE165ot7tahOd1VZ1+uXbeGqy/GFM2uJ9Q/MTkStYZ4gITWc3/davB3NKJVcJN7xQR5/pNPMspR7PgCU2UhgWNFQuVHPhp9fiokIaR+QyqAOtQdd0nd6oFAIsKRPIBDjooo1sKE4BaXDHdQibNSBEdeJRgv1DjwtX77wEtXoV8DvS3+Z0sH2FJeY+iAY2bB2Q=
-----------------------------680184
Content-Disposition: form-data; name="ts"

1472985387451
-----------------------------680184--

所以它几乎完全相同,除了没有保持活着的事实,我不知道为什么考虑它是在我的请求中写的。 但是,这是我的代码:

string Html = GetHtml("http://ask. fm/account/settings/profile");
string specs = Utils.GetSpecs(Html);
string ts = Utils.GetTs(Html);

Bitmap Image = new Bitmap(ImagePath);
MemoryStream mStream = new MemoryStream();
Image.Save(mStream, ImageFormat.Jpeg);
byte[] Bytes = mStream.ToArray();

string Boundary = "-----------------------------" + rnd.Next(100000, 900000);
string HeaderTemplate = @"Content-Disposition: form-data; name=""file""; filename=""1.png""\r\nContent-Type: ""image/png""\r\n\r\n";

HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("http://upload5 .ask.fm/upload/api-avatar");
postReq.AutomaticDecompression = DecompressionMethods.GZip;
WebHeaderCollection postHeaders = postReq.Headers;
postReq.Method = "POST";
postReq.Host = "upload5 .ask.fm";
postReq.ServicePoint.Expect100Continue = false;
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0";
postReq.Accept = "*/*";
postHeaders.Add("Accept-Language", "it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3");
postHeaders.Add("Accept-Encoding", "gzip, deflate");
postReq.Referer = "http://ask .fm/account/settings/profile";
postHeaders.Add("Origin", "http://ask .fm");
postHeaders.Add("DNT", "1");
postReq.ContentType = "multipart/form-data; boundary=" + Boundary;
postReq.CookieContainer = Cookies;
postReq.KeepAlive = true;

string BoundaryHeader = Boundary + Environment.NewLine + @"Content-Disposition: form-data; name=""file""; filename=""{0}""" + Environment.NewLine + "Content-Type: image/png" + Environment.NewLine + Environment.NewLine;
string BoundaryEnd = Environment.NewLine + Boundary + Environment.NewLine + @"Content-Disposition: form-data; name=""specs""" + Environment.NewLine + Environment.NewLine + "{0}" + Environment.NewLine + Boundary + Environment.NewLine + @"Content-Disposition: form-data; name=""ts""" + Environment.NewLine + Environment.NewLine + "{1}" + Environment.NewLine + Boundary + "--";

byte[] BoundaryHeaderBytes = Encoding.UTF8.GetBytes(string.Format(BoundaryHeader, Path.GetFileName(ImagePath)));
byte[] BoundaryEndBytes = Encoding.ASCII.GetBytes(string.Format(BoundaryEnd, specs, ts));

FileStream fileStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
long length = BoundaryHeaderBytes.Length + fileStream.Length + BoundaryEndBytes.Length;
postReq.ContentLength = length;
Stream requestStream = postReq.GetRequestStream();
requestStream.Write(BoundaryHeaderBytes, 0, BoundaryHeaderBytes.Length);
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
        requestStream.Write(buffer, 0, bytesRead);
requestStream.Write(BoundaryEndBytes, 0, BoundaryEndBytes.Length);
WebResponse responce = postReq.GetResponse();
Stream s = responce.GetResponseStream();
StreamReader sr = new StreamReader(s);

问题是什么?有一天,我正在努力解决这个问题,但我还没有找到解决方案,请提前感谢您的任何答案!

0 个答案:

没有答案