/// <summary> /// 上传头像 /// </summary> /// <param name="web"></param> /// <param name="cloneUid"></param> /// <returns></returns> public static string UploadAvatar(WebAccessBase web, string cloneUid) { var web2 = new WebAccessBase(); var userStr = GetCnPage.GetUser(web2, cloneUid); var user = AnalyseCnPage.AnalysisUserHome(userStr); if (user == null) { return "上传头像 访问克隆对象页面失败"; } var avatarurl = user.ProfileImageUrl; byte[] picBytes = web2.GetImageByte(avatarurl); if (picBytes == null) { return "上传头像 访问克隆对象头像失败"; } File.WriteAllBytes("0.jpg", picBytes); web.Reffer = null; web.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"; var html1 = web.GetHTML("http://weibo.cn"); if (string.IsNullOrEmpty(html1)) { return "上传头像 访问主页失败"; } var setUrl = Regex.Match(html1, @"<a href=""(?<url>.*?)"">设置</a>").Groups["url"].Value; if (string.IsNullOrEmpty(setUrl)) { return "上传头像 分析设置页地址失败"; } setUrl = "http://weibo.cn" + setUrl.Replace("&", "&"); var html2 = web.GetHTML(setUrl); if (string.IsNullOrEmpty(html2)) { return "上传头像 访问设置页失败"; } var dataUrl = Regex.Match(html2, @"<a href=""(?<url>[^""]*?)"">资料</a>").Groups["url"].Value; if (string.IsNullOrEmpty(dataUrl)) { return "上传头像 分析资料页地址失败"; } dataUrl = "http://weibo.cn" + dataUrl; var html3 = web.GetHTML(dataUrl); if (string.IsNullOrEmpty(html3)) { return "上传头像 访问资料页失败"; } var avatarUrl = Regex.Match(html3, @"<a href=""(?<url>[^""]*?)"">头像</a>").Groups["url"].Value; if (string.IsNullOrEmpty(avatarUrl)) { return "上传头像 分析头像页地址失败"; } avatarUrl = "http://weibo.cn" + avatarUrl; var html4 = web.GetHTML(avatarUrl); if (string.IsNullOrEmpty(html4)) { return "上传头像 访问头像页失败"; } var avatarPostUrl = Regex.Match(html4, @"form action=""(?<url>[^""]*?)""").Groups["url"].Value; if (string.IsNullOrEmpty(avatarPostUrl)) { return "上传头像 分析头像上传地址失败"; } avatarPostUrl = "http://weibo.cn" + avatarPostUrl.Replace("&", "&"); var result = web.UploadImage(picBytes, avatarPostUrl, "pic", "image/jpeg", "Content-Disposition: form-data; name=\"act\"\r\n\r\navatar&Content-Disposition: form-data; name=\"save\"\r\n\r\n1"); if (!string.IsNullOrEmpty(result) && result.Contains("<div class=\"ps\">设置成功</div>")) return ""; CNHttpWorkLogger.Info("上传头像失败\r\n{0}", result); return "上传头像 上传失败"; }