如图,这是成功手动上传图片的抓包截图,上面是提交的数据,下半部分是返回的数据。
刚学习了http://www.sufeinet.com/thread-8073-1-1.html 着篇帖子,模仿DEMO提交上传的数据。
分析提交的数据,自己把post提交的数据分成 两部分,第一部分是
------WebKitFormBoundaryVaIeygBO2zXVSvDm
Content-Disposition: form-data; name="name"
test.jpg
------WebKitFormBoundaryVaIeygBO2zXVSvDm
Content-Disposition: form-data; name="action"
upload-attachment
------WebKitFormBoundaryVaIeygBO2zXVSvDm
Content-Disposition: form-data; name="_wpnonce"
122ccecd91
------WebKitFormBoundaryVaIeygBO2zXVSvDm
Content-Disposition: form-data; name="post_id"
2722
------WebKitFormBoundaryVaIeygBO2zXVSvDm
Content-Disposition: form-data; name="async-upload"; filename="test.jpg"
Content-Type: image/jpeg
这部分参照DEMO转化成byte[]
第二部分抓包的显示是乱码,如上图是 四个方框 黏贴抓包的数据到下面是如下。
����
这部分是不是就是图片的数据,抓包没法显示所以就是乱码了。
按照上面的分析,基本上post的内容就是两部分,第一部分就是拼接的string转byte[],第二部分是图片转byte[]。给予上述分析,测试用软件提交。返回的html如下:
[HTML] 纯文本查看 复制代码 <!DOCTYPE html>
<!-- Ticket #11289, IE bug fix: always pad the error page with enough characters such that it is greater than 512 bytes, even after gzip compression abcdefghijklmnopqrstuvwxyz1234567890aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz11223344556677889900abacbcbdcdcededfefegfgfhghgihihjijikjkjlklkmlmlnmnmononpopoqpqprqrqsrsrtstsubcbcdcdedefefgfabcadefbghicjkldmnoepqrfstugvwxhyz1i234j567k890laabmbccnddeoeffpgghqhiirjjksklltmmnunoovppqwqrrxsstytuuzvvw0wxx1yyz2z113223434455666777889890091abc2def3ghi4jkl5mno6pqr7stu8vwx9yz11aab2bcc3dd4ee5ff6gg7hh8ii9j0jk1kl2lmm3nnoo4p5pq6qrr7ss8tt9uuvv0wwx1x2yyzz13aba4cbcb5dcdc6dedfef8egf9gfh0ghg1ihi2hji3jik4jkj5lkl6kml7mln8mnm9ono
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WordPress错误提醒</title>
<style type="text/css">
html {
background: #f1f1f1;
}
body {
background: #fff;
color: #444;
font-family: "Open Sans", sans-serif;
margin: 2em auto;
padding: 1em 2em;
max-width: 700px;
-webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);
box-shadow: 0 1px 3px rgba(0,0,0,0.13);
}
h1 {
border-bottom: 1px solid #dadada;
clear: both;
color: #666;
font: 24px "Open Sans", sans-serif;
margin: 30px 0 0 0;
padding: 0;
padding-bottom: 7px;
}
#error-page {
margin-top: 50px;
}
#error-page p {
font-size: 14px;
line-height: 1.5;
margin: 25px 0 20px;
}
#error-page code {
font-family: Consolas, Monaco, monospace;
}
ul li {
margin-bottom: 10px;
font-size: 14px ;
}
a {
color: #21759B;
text-decoration: none;
}
a:hover {
color: #D54E21;
}
.button {
background: #f7f7f7;
border: 1px solid #cccccc;
color: #555;
display: inline-block;
text-decoration: none;
font-size: 13px;
line-height: 26px;
height: 28px;
margin: 0;
padding: 0 10px 1px;
cursor: pointer;
-webkit-border-radius: 3px;
-webkit-appearance: none;
border-radius: 3px;
white-space: nowrap;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
-webkit-box-shadow: inset 0 1px 0 #fff, 0 1px 0 rgba(0,0,0,.08);
box-shadow: inset 0 1px 0 #fff, 0 1px 0 rgba(0,0,0,.08);
vertical-align: top;
}
.button.button-large {
height: 29px;
line-height: 28px;
padding: 0 12px;
}
.button:hover,
.button:focus {
background: #fafafa;
border-color: #999;
color: #222;
}
.button:focus {
-webkit-box-shadow: 1px 1px 1px rgba(0,0,0,.2);
box-shadow: 1px 1px 1px rgba(0,0,0,.2);
}
.button:active {
background: #eee;
border-color: #999;
color: #333;
-webkit-box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
}
</style>
</head>
<body id="error-page">
<p>您确定要这样做?</p><p><a href='http://www.aaa.com/wp-admin/post-new.php'>请重试。</a></p></body>
</html>
显然,提交没有成功,手动登陆wordpress查看也的确提交没有成功。
提交部分的c#代码如下:
[C#] 纯文本查看 复制代码 #region 变量
byte[] UploadBuffers = null;
string BoundStr = "------WebKitFormBoundaryTUZNUmq1MdcleafM";//根据抓包生成
StringBuilder UploadBuf = new StringBuilder();
#endregion
#region 头部数据
UploadBuf.Append(BoundStr + "\r\n" + "Content-Disposition: form-data; name=\"name\" \r\n\r\ntest.jpg\r\n");
UploadBuf.Append(BoundStr + "\r\n" + "Content-Disposition: form-data; name=\"action\" \r\n\r\nupload-attachment\r\n");
UploadBuf.Append(BoundStr + "\r\n" + "Content-Disposition: form-data; name=\"_wpnonce\" \r\n\r\n" + _wpnonce + "\r\n");
UploadBuf.Append(BoundStr + "\r\n" + "Content-Disposition: form-data; name=\"post_id\" \r\n\r\n" + post_ID + "\r\n");
UploadBuf.Append(BoundStr + "\r\n" + "Content-Disposition: form-data; name=\"async-upload\"; filename=\"test.jpg\" \r\n");
UploadBuf.Append("Content-Type: image/jpeg \r\n\r\n");
string headstr = UploadBuf.ToString();
byte[] HeadBytes = Encoding.ASCII.GetBytes(headstr);
#endregion
#region 图片数据
byte[] PicBytes = ImageToBytesFromFilePath(@"C:\Users\li\Desktop\test.jpg");
#endregion
#region 尾部数据
//UploadBuf = new StringBuilder();
////UploadBuf.Append(BoundStr + "--");
//byte[] TailBytes = Encoding.ASCII.GetBytes(UploadBuf.ToString());
#endregion
#region 数组拼接
UploadBuffers = ComposeArrays(HeadBytes, PicBytes);
#endregion
#region 上传
item = new HttpItem()
{
URL = "http://www.duxiuyin.com/wp-admin/async-upload.php",
Method = "POST",
ContentType = "multipart/form-data; boundary=" + BoundStr.Substring(2),
Referer = "http://www.duxiuyin.com/wp-admin/post-new.php",
PostDataType = PostDataType.Byte,
PostEncoding = Encoding.UTF8,
PostdataByte = UploadBuffers
};
item.Accept = "*/*";
item.Encoding = Encoding.UTF8;
item.UserAgent = this.UserAgent;
item.KeepAlive = true;
item.Expect100Continue = false;
item.CookieCollection = this.curCookies;
item.ResultType = ResultType.String;
item.ResultCookieType = ResultCookieType.CookieCollection;
item.Allowautoredirect = false;
item.Connectionlimit = 1000;
result = http.GetHtml(item);
richTextBox1.Text=result.Html;
#endregion
个人感觉可能的原因有:
1.就是对自己对POST的byte[]数据处理不自信,似乎问题应该在这里。
2.头信息的话感觉应该问题不大。
3.另外,每次上传一张图片时,包括同一词登陆,string BoundStr = "------WebKitFormBoundaryTUZNUmq1MdcleafM";// TUZNUmq1MdcleafM这个值是变化的,搜索抓包信息除了提交页面外找不到,应该是js生成的,但感觉似乎问题也不太大。但也不除外是这里每次测试都用的固定值导致的(那段JS好长,用chinaz的格式化工具处理后其实也没全部格式化,关键就是生成TUZNUmq1MdcleafM的那段没能格式化,所以就偷懒没分析JS了)。
对于http://www.sufeinet.com/thread-8073-1-1.html 的学习还不够深刻,但是由于DEMO中的网址因为阿里巴巴的改版也发生了变化,所以没法找到对应的页面抓包对比测试学习一下,所以只好来这里提问了。先谢谢所有抽出自己的时间回我帖子的朋友们了!
|