|
本帖最后由 jsjrsxpwr 于 2014-4-17 16:27 编辑
@站长苏飞 首先感谢站长帮我解决了PHPWIND论坛登陆的问题,随即又产生了新的问题,那就是如何上传图片到PHpwind论坛。
还是以mydigit.cn为例抓取PHPWIND论坛上传图片的数据包:
浏览器头信息:Content-Type: multipart/form-data; boundary=----------cH2ae0GI3gL6ei4ae0Ij5Ij5ae0KM7
post数据包:
------------cH2ae0GI3gL6ei4ae0Ij5Ij5ae0KM7
Content-Disposition: form-data; name="Filename"
123.JPG
------------cH2ae0GI3gL6ei4ae0Ij5Ij5ae0KM7
Content-Disposition: form-data; name="uid"
461845
------------cH2ae0GI3gL6ei4ae0Ij5Ij5ae0KM7
Content-Disposition: form-data; name="verify"
4f2785cb
------------cH2ae0GI3gL6ei4ae0Ij5Ij5ae0KM7
Content-Disposition: form-data; name="step"
2
------------cH2ae0GI3gL6ei4ae0Ij5Ij5ae0KM7
Content-Disposition: form-data; name="fid"
85
------------cH2ae0GI3gL6ei4ae0Ij5Ij5ae0KM7
Content-Disposition: form-data; name="desc"
123.JPG
------------cH2ae0GI3gL6ei4ae0Ij5Ij5ae0KM7
Content-Disposition: form-data; name="Filedata"; filename="123.JPG"
Content-Type: application/octet-stream
文件信息 ------------cH2ae0GI3gL6ei4ae0Ij5Ij5ae0KM7
Content-Disposition: form-data; name="Upload"
Submit Query
------------cH2ae0GI3gL6ei4ae0Ij5Ij5ae0KM7--
返回数据为:
<?xml version="1.0" encoding="gbk"?><ajax><![CDATA[{"aid":"3953401","path":"http://img.mydigit.net/Mon_1404/85_461845_0fc75deaf1018ce.jpg"}]]></ajax>
百度一圈后,发现论坛有类似的问题,仿照前面的代码写了一段程序,上传到图片到论坛结果返回数据为
{"uid":"461845","step":2,"verify":"4f2785cb"}
说明:问题应该和cookie没有关系,问题是出在我的文件构造上呢?还是其它?
附上抄过来改写的代码HttpUploadFile("http://bbs.mydigit.cn/job.php?action=mutiupload&random=92&photoid=0&aid=0&t=" + gettime(), "Filename=123.JPG&desc=123.JPG&fid=85&verify=" + verify + "&uid=" + uid + "&step=2" , "Upload=Submit Query", "Filedata", "123.JPG", "application/octet-stream", cookie, "http://bbs.mydigit.cn/post.php?fid=85");
public string HttpUploadFile(string url, string headpoststr, string afterfilepoststr, string fileformname, string filepath, string fileType, CookieCollection cookie, string refre)
{
String fileToUpload = filepath;
String uploadUrl = url;
String fileFormName = fileformname;
String contenttype = fileType;
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl);
webrequest.CookieContainer=new CookieContainer();
webrequest.CookieContainer.Add(cookie);
webrequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
StringBuilder sb = new StringBuilder();
foreach (string c in headpoststr.Split('&'))
{
string[] item = c.Split('=');
if (item.Length != 2)
{
break;
}
string name = item[0];
string value = item[1];
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"" + name + "\"");
sb.Append("\r\n\r\n");
sb.Append(value);
sb.Append("\r\n");
}
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append(fileFormName);
sb.Append("\"; filename=\"");
sb.Append(Path.GetFileName(fileToUpload));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append(contenttype);
sb.Append("\r\n");
sb.Append("\r\n");
string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);//头信息
sb = new StringBuilder();
sb.Append("\r\n");
foreach (string c in afterfilepoststr.Split('&'))
{
string[] item = c.Split('=');
if (item.Length != 2)
{
break;
}
string name = item[0];
string value = item[1];
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"" + name + "\"");
sb.Append("\r\n\r\n");
sb.Append(value);
sb.Append("\r\n");
}
string afterfileHeader = sb.ToString();
byte[] afterfileHeaderBytes = Encoding.UTF8.GetBytes(afterfileHeader);//文件后面的信息
byte[] boundaryBytes = Encoding.ASCII.GetBytes(boundary + "--");//结束信息
FileStream fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + fileStream.Length +afterfileHeaderBytes.Length + boundaryBytes.Length;
webrequest.ContentLength = length;
Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);//写头信息
byte[] buffer = new Byte[(int)fileStream.Length];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);// 写文件信息
}
requestStream.Write(afterfileHeaderBytes, 0, afterfileHeaderBytes.Length);//写文件后信息
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);//结束信息
WebResponse responce = webrequest.GetResponse();
requestStream.Close();
using (Stream s = responce.GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
return sr.ReadToEnd();
}
}
}
|
|