public static UploadMediaRequest Get(String toUser, Session session, String filePath) { UploadMediaRequest result = new UploadMediaRequest(); result.BaseRequest = session.BaseRequest; result.FromUserName = session.CurrentUser.UserName; result.ToUserName = toUser; result.UploadType = 2; result.ClientMediaId = Common.GetTimeStamp(); System.IO.FileInfo finfo = new System.IO.FileInfo(filePath); result.TotalLen = finfo.Length; result.MediaType = 4; result.StartPos = 0; result.DataLen = finfo.Length; result.FileMd5 = GetMD5HashFromFile(filePath).ToLower(); return(result); }
/// <summary> /// 上传媒体文件,如果上传成功,返回获取到的MediaID,否则,返回String.Empty /// </summary> private String UploadMedia(String file, String receiver, Int32 fileIndex = 0) { String toUser = receiver; System.Net.ServicePointManager.DefaultConnectionLimit = 100; //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications); String _result = String.Empty; String _url = $"https://file.{m_Session.WXVer}.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json"; HttpWebRequest _request = WebRequest.CreateHttp(_url); System.IO.FileStream fs = System.IO.File.Open(file, System.IO.FileMode.Open); byte[] content = new byte[fs.Length]; fs.Read(content, 0, content.Length); fs.Close(); //_request.KeepAlive = false; _request.CookieContainer = m_Session.CookieContainer; _request.Timeout = (Int32)TimeSpan.FromSeconds(20).TotalMilliseconds; _request.ProtocolVersion = HttpVersion.Version10; _request.Method = "POST"; _request.UserAgent = m_Session.UserAgent; _request.Accept = "*/*"; String boundary = "----WebKitFormBoundaryXfoMzS2vayvACSgy"; //_request.ContentType = "application/x-www-form-urlencoded"; _request.ContentType = "multipart/form-data; boundary=" + boundary; //_request.ContentType = "multipart/form-data;"; //_request.ContentLength = content.Length; System.IO.FileInfo finfo = new System.IO.FileInfo(file); Dictionary <string, object> paras = new Dictionary <string, object>(); paras.Add("id", "WU_FILE_" + fileIndex); paras.Add("name", finfo.Name); paras.Add("lastModifiedDate", finfo.LastWriteTimeUtc); paras.Add("size", finfo.Length); paras.Add("mediatype", "pic"); paras.Add("uploadmediarequest", JsonConvert.SerializeObject(UploadMediaRequest.Get(toUser, m_Session, file))); paras.Add("webwx_data_ticket", m_Session.GetCookie("webwx_data_ticket")); paras.Add("pass_ticket", m_Session.pass_ticket); paras.Add("filename", new FileParameter(content, finfo.Name, "image/jpeg")); byte[] postData = GetMultipartFormData(paras, boundary); System.IO.Stream _stream = _request.GetRequestStream(); _stream.Write(postData, 0, postData.Length); _stream.Close(); HttpWebResponse response = (HttpWebResponse)_request.GetResponse(); m_Session.AddCookies(response.Cookies); if (response.StatusCode != HttpStatusCode.OK) { WriteLog("[上传文件]HTTP加载错误,HTTP请求状态:" + response.StatusCode); } System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()); String _responseText = sr.ReadToEnd(); sr.Close(); response.Close(); WriteLog("[上传文件]HTTP请求返回结果:" + _responseText); if (_responseText.Contains("\"MediaId\":")) { String mediaID = Regex.Match(_responseText, "(?<=\"MediaId\":).{1,}(?=\",)").Value; _result = mediaID; } return(_result); }