亲们,我现在在做一个模拟发布腾讯微博的小工具,通过抓包已经可以正常登陆并发布文字微博了,现在卡在图片上传上面,微博首页上传图片用的是flash上传,用Fiddler抓包分析后模拟上传出现问题,传递的参数都和网页上传的参数一一对应,但是上传结果总是提示参数错误,麻烦高手看一下,我把源码发上来:
[C#] 纯文本查看 复制代码 /// <summary>
/// 上传图片
/// </summary>
/// <returns></returns>
public string uploadImage()
{
string picpath = "D://05513a251-11.jpg";
FileStream file = new FileStream(picpath, FileMode.Open);
byte[] bb = new byte[file.Length];
file.Read(bb, 0, (int)file.Length);
file.Close();
pictureBox2.Image = byteArrayToImage(bb);
MsMultiPartFormData form = new MsMultiPartFormData();
form.AddFormField("Filename", Path.GetFileName(picpath));
form.AddStreamFile("filename", Path.GetFileName(picpath), bb);
form.AddFormField("Upload", "Submit Query");
form.PrepareFormData();
form.GetFormData();
host = "upload.t.qq.com";
url = string.Format("http://upload.t.qq.com/asyn/uploadpicCommon.php?call=2&uin={0}&g_tk={1}&rand={2}&_ps1={3}&_ps2=null", uin, weiboGtk, rand.NextDouble(), _ps1);
ContentType = "multipart/form-data; boundary=" + form.Boundary;
help = new HttpHelper();
item = new HttpItem()
{
URL = url,
CookieCollection = cookieCollection,
ResultCookieType = ResultCookieType.CookieCollection,
Host = host,
Accept = "text/*",
UserAgent = "Shockwave Flash",
ContentType = ContentType,
Method = "POST",
PostDataType = PostDataType.Byte,
PostdataByte = form.GetFormData().ToArray(),
Encoding = Encoding.UTF8,
ProxyIp = "ieproxy"
};
item.Header.Add("Pragma", "no-cache");
item.Header.Add("DNT", "1");
result = help.GetHtml(item);
this.BeginInvoke(updateLog, "图片上传结果:" + result.Html);
return result.Html;
}
上面是上传的过程,另外用到了:
[C#] 纯文本查看 复制代码 class MsMultiPartFormData
{
private List<byte> formData;
public String Boundary = "----------cH2cH2gL6GI3ae0Ef1ae0ae0ae0cH2";
private String fieldName = "Content-Disposition: form-data; name=\"{0}\"";
private String fileContentType = "Content-Type: {0}";
private String fileField = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"";
private Encoding encode = Encoding.GetEncoding("UTF-8");
public MsMultiPartFormData()
{
formData = new List<byte>();
}
public void AddFormField(String FieldName, String FieldValue)
{
String newFieldName = fieldName;
newFieldName = string.Format(newFieldName, FieldName);
formData.AddRange(encode.GetBytes("--" + Boundary + "\r\n"));
formData.AddRange(encode.GetBytes(newFieldName + "\r\n\r\n"));
formData.AddRange(encode.GetBytes(FieldValue + "\r\n"));
}
public void AddFile(String FieldName, String FileName, byte[] FileContent, String ContentType)
{
String newFileField = fileField;
String newFileContentType = fileContentType;
newFileField = string.Format(newFileField, FieldName, FileName);
newFileContentType = string.Format(newFileContentType, ContentType);
formData.AddRange(encode.GetBytes("--" + Boundary + "\r\n"));
formData.AddRange(encode.GetBytes(newFileField + "\r\n"));
formData.AddRange(encode.GetBytes(newFileContentType + "\r\n\r\n"));
formData.AddRange(FileContent);
formData.AddRange(encode.GetBytes("\r\n"));
}
public void AddStreamFile(String FieldName, String FileName, byte[] FileContent)
{
AddFile(FieldName, FileName, FileContent, "application/octet-stream");
}
public void PrepareFormData()
{
formData.AddRange(encode.GetBytes("--" + Boundary + "--"));
}
public List<byte> GetFormData()
{
return formData;
}
}
Cookie的话,能成功发布文字微博理论上不会有问题,POST提交的参数也对应,难道是在flash里面做了验证吗?
|