发表说说之前,必须登录。
模拟QQ登录 >> http://www.cnblogs.com/deeround/p/4386629.html
发表带图说说,自然少不了上传图片,我这使用的PC端上传图片
首先,FileHelper的创建
QQ空间上传图片,使用的flash上传图片,所以我们需要在post的时候,提交的数据进行一个转换。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web; namespace Web.QQ
{
class FileHelper
{
private List<byte> formData;
private Encoding encode = Encoding.GetEncoding("UTF-8");
public FileHelper()
{
formData = new List<byte>();
}
public void Add(string name, string value)
{
formData.AddRange(encode.GetBytes("--dnpbajwbhbccmrkegkhtrdxgnppkncfv\r\n"));
formData.AddRange(encode.GetBytes("Content-Disposition: form-data; name=\"" + name + "\"\r\n"));
formData.AddRange(encode.GetBytes("\r\n"));
formData.AddRange(encode.GetBytes(value + "\r\n"));
}
public void Add(string name, string fileName, byte[] fileData)
{
formData.AddRange(encode.GetBytes("--dnpbajwbhbccmrkegkhtrdxgnppkncfv\r\n"));
formData.AddRange(encode.GetBytes("Content-Disposition: form-data; name=\"filename\"; filename=\"" + fileName + "\"\r\n"));
formData.AddRange(encode.GetBytes("Content-Type: application/octet-stream\r\n"));
formData.AddRange(encode.GetBytes("\r\n"));
formData.AddRange(fileData);
formData.AddRange(encode.GetBytes("\r\n"));
}
public void Add()
{
formData.AddRange(encode.GetBytes("--dnpbajwbhbccmrkegkhtrdxgnppkncfv--"));
}
public List<byte> GetFormData()
{
Add();
return formData;
}
}
}
接着,一个生产图片提交的数据方法
public string UploadImage(string qq, string filePath, Model model)
{
string skey = GetCookieString(model.CookieContainer, "skey"); FileStream file = new FileStream(filePath, FileMode.Open);
byte[] bb = new byte[file.Length];
file.Read(bb, , (int)file.Length);
file.Close(); FileHelper form = new FileHelper();
form.Add("hd_quality", "");
form.Add("hd_height", "");
form.Add("filename", "filename");
form.Add("upload_hd", "");
form.Add("hd_width", "");
form.Add("charset", "utf-8");
form.Add("output_type", "xml");
form.Add("uin", qq);
form.Add("output_charset", "utf-8");
form.Add("albumtype", "");
form.Add("exif_info", "extendXml:");
form.Add("skey", skey);
form.Add("zzpaneluin", qq);
form.Add("refer", "shuoshuo");
form.Add("uploadtype", "");
form.Add("photoData", "filename");
form.Add("Filename", Path.GetFileName(filePath));
form.Add("filename", Path.GetFileName(filePath), bb);
form.Add("Upload", "Submit Query");
form.GetFormData(); string url = "http://shup.photo.qq.com/cgi-bin/upload/cgi_upload_image";
string html = new Helper().Post(url, form.GetFormData().ToArray(), model.CookieContainer); return html;
}
接着,发表说说的方法
#region 空间动作
public void PublishShuoShuo(string qq,Model model)
{
string gtk = new Helper().GetGtk(GetCookieString(model.CookieContainer, "skey"));
string url = "http://taotao.qq.com/cgi-bin/emotion_cgi_publish_v6?g_tk=" + gtk;
string content = "我是机器人,请不要为我点赞~~";
string postData = "qzreferrer=http%3A%2F%2Fuser.qzone.qq.com%2F" + qq + "&syn_tweet_verson=1¶mstr=1&pic_template=&richtype=&richval=&special_url=&subrichtype=&con=" + HttpUtility.UrlEncode(content) + "&feedversion=1&ver=1&ugc_right=1&to_tweet=0&to_sign=0&hostuin=" + qq + "&code_version=1&format=fs"; string html = new Helper().Post(url, postData, model.CookieContainer);
}
public void PublishShuoShuo(string qq, string filePath, Model model)
{
//先上传图片
string html = UploadImage(qq, filePath, model); IDictionary<string, string> data = new Dictionary<string, string>();
string[] lines = html.Replace("\n", "|").Split('|');
foreach (var line in lines)
{
Regex regex = new Regex("<(.+)>(.*)</.+>");
MatchCollection mc = regex.Matches(line);
foreach (Match m in mc)
{
if (m.Groups.Count > )
{
data.Add(m.Groups[].Value, m.Groups[].Value);
}
}
} string gtk = new Helper().GetGtk(GetCookieString(model.CookieContainer, "skey"));
string url = "http://taotao.qq.com/cgi-bin/emotion_cgi_publish_v6?g_tk=" + gtk;
string richval = string.Format(",{0},{1},{2},{3},{4},{5},,{4},{5}", data["albumid"], data["lloc"], data["sloc"], data["type"], data["height"], data["width"]);
string picbo = string.Format("{0} {1}", data["pre"].Substring(data["pre"].IndexOf("bo=") + ), data["url"].Substring(data["url"].IndexOf("bo=") + ));
string content = "我是机器人,请不要为我点赞~~";
string postData = "qzreferrer=http%3A%2F%2Fuser.qzone.qq.com%2F" + qq + "&syn_tweet_verson=1¶mstr=1&pic_template=&richtype=1&richval=" + HttpUtility.UrlEncode(richval) + "&special_url=&subrichtype=1&pic_bo=" + HttpUtility.UrlEncode(picbo) + "&con=" + content + "&feedversion=1&ver=1&ugc_right=1&to_tweet=0&to_sign=0&hostuin=" + qq + "&code_version=1&format=fs"; string html1 = new Helper().Post(url, postData, model.CookieContainer);
} #endregion
最后,如何调用
new Methods().PublishShuoShuo(qq, "D:\\1.png", model);
到此结束,目前只能发送一张图,发表多图说说的话,应该是多执行几次UploadImage,同时生产post数据也需要相应的修改,不过这个还没做。