POST 文件上传问题。
这次要做这个。post百度网盘的文件。模拟网页版的百度网盘上传文件。有一段代码是这样的。
------------Ij5ei4ae0ei4cH2ae0Ef1ei4Ij5gL6
Content-Disposition: form-data; name="Filename"
1.bmp
------------Ij5ei4ae0ei4cH2ae0Ef1ei4Ij5gL6
Content-Disposition: form-data; name="Filedata"; filename="1.bmp"
Content-Type: application/octet-stream
------------Ij5ei4ae0ei4cH2ae0Ef1ei4Ij5gL6
Content-Disposition: form-data; name="Upload"
Submit Query
------------Ij5ei4ae0ei4cH2ae0Ef1ei4Ij5gL6--
我的代码是这样的。为什么一直无法发送出去呢?我的cookie是自己截取来的。
public bool postFile(string Url, string cookieStr, string StrPostFileName, string host, string referer,string fileName)--------------------编程问答-------------------- 一直都无法成功发送。大家来看看,我哪里错了么? --------------------编程问答-------------------- 最后一个 strBoundary 应该有两个 - 结束。 --------------------编程问答-------------------- 抓包正确吗? --------------------编程问答--------------------
{
//1.读入一个文件。
FileStream fs = new FileStream(StrPostFileName, FileMode.Open, FileAccess.Read);
//2.将读入的文件保存为二进制文件。
BinaryReader r = new BinaryReader(fs);
//3.http header头
string strBoundary = "------------Ij5ei4ae0ei4cH2ae0Ef1ei4Ij5gL6"; //"----------" + DateTime.Now.Ticks.ToString("x");
//byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");
StringBuilder sb = new StringBuilder();
sb.Append(strBoundary);
sb.Append("\r\nContent-Disposition: form-data; name=\"Filename\"");
sb.Append("\r\n");
sb.Append(fileName);
sb.Append("\r\n");
sb.Append(strBoundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"Filedata\"; filename=" + fileName + "\"");
sb.Append("\r\nContent-Type: application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append(strBoundary);
sb.Append("\r\nContent-Disposition: form-data; name=\"Upload\"");
sb.Append("\r\n");
sb.Append("\r\nSubmit Query");
sb.Append("\r\n");
sb.Append(strBoundary);
string strPostHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
//根据uri创建httpWebRequest对象
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(Url));
httpReq.Method = "POST";
httpReq.Accept = "*/*";
httpReq.Timeout = 300;
httpReq.Referer = referer;
httpReq.ContentType = "multipart/form-data; boundary="+strBoundary;
long length = fs.Length + postHeaderBytes.Length;
long fileLength = fs.Length;
httpReq.ContentLength = length;
CookieContainer objcok = new CookieContainer();
string[] st = cookieStr.Split(';');
foreach (string s in st)
{
int i = 0;
i = s.IndexOf('=');
Cookie ck = new Cookie();
ck.Name = s.Substring(0, i).Trim();
ck.Value = s.Substring(i + 1, s.Length - i - 1).Trim();
objcok.Add(new Uri(host), ck);
}
httpReq.CookieContainer = objcok;
try
{
//每次上传1k;
int bufferLength = 1024;
byte[] buffer = new byte[bufferLength];
//已上传的字节数
long offset = 0;
//开始上传时间
DateTime startTime = DateTime.Now;
int size = r.Read(buffer, 0, bufferLength);
Stream postStream = httpReq.GetRequestStream();
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
while (size > 0)
{
postStream.Write(buffer, 0, size);
offset += size;
//Application.DoEvents();
size = r.Read(buffer, 0, bufferLength);
}
postStream.Close();
}
catch
{
return false;
}
finally
{
fs.Close();
r.Close();
}
return true;
}
抓包正确。发送过去后,捉包软件,也能捉到。只是,发送一下,就没反应了。= = --------------------编程问答-------------------- 加我 可以帮你看看 qq 1351730 --------------------编程问答-------------------- 大哥,不让我加啊,拒绝添加好友。。 --------------------编程问答-------------------- 这个是模拟百度网盘上传文件的,只要求能传100KB以下的就行了。能传大文件也行。。
补充:.NET技术 , C#