private UpoladType GetUpooadType(FileInfo fi) { UpoladType upoladType = new UpoladType(); upoladType.FileInfo = fi; string fileExtension = fi.Extension; switch (fileExtension.ToLower()) { case ".jpg": case ".jpge": upoladType.type = "image/jpg"; upoladType.ContentType = "image/jpg"; upoladType.mediatype = "pic"; break; case "gif": upoladType.type = "image/gif"; upoladType.ContentType = "image/gif"; upoladType.mediatype = "doc"; break; case ".mp4": upoladType.type = "video/mp4"; upoladType.ContentType = "video/mp4"; upoladType.mediatype = "video"; break; case ".doc": case ".docx": case ".ppt": case ".pptx": case ".xls": case ".xlsx": upoladType.type = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; upoladType.ContentType = "image/gif"; upoladType.mediatype = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; break; case ".txt": upoladType.type = "text/plain"; upoladType.ContentType = "text/plain"; upoladType.mediatype = "doc"; break; default: upoladType.type = "application/octet-stream"; upoladType.ContentType = "application/octet-stream"; upoladType.mediatype = "doc"; break; } return(upoladType); }
public string UploadFile(string path) { Cookie sid = HttpServer.GetCookie("wxsid"); Cookie uin = HttpServer.GetCookie("wxuin"); FileInfo file = new FileInfo(path); StringBuilder sb = new StringBuilder(); sb.AppendFormat("{{\"UploadType\":2,\"BaseRequest\":{{\"Uin\":{0},\"Sid\":\"{1}\",\"Skey\":\"{2}\",\"DeviceID\":\"e{3}\"}},", uin.Value, sid.Value, LoginService.SKey, Utils.GetTimeSpan()); sb.AppendFormat("\"ClientMediaId\":{3},\"TotalLen\":{0},\"StartPos\":0,\"DataLen\":{1},\"MediaType\":4,\"FileMd5\":\"{2}\"}}", file.Length, file.Length, GetFileMD5Hash.GetMD5Hash(path), Utils.GetTimeSpan()); UpoladType uploadType = GetUpooadType(file); uploadType.uploadmediarequest = sb.ToString(); uploadType.Url = _uplpadFileUrl; byte[] bytes = HttpServer.SendPostRequestByFile(uploadType); string send_result = string.Empty; if (bytes != null) { send_result = Encoding.UTF8.GetString(bytes); } return(send_result); }
public static byte[] SendPostRequestByFile(UpoladType upload) { Cookie webwx_data_ticket = HttpServer.GetCookie("webwx_data_ticket"); string filename = upload.FileInfo.Name; long filesize = upload.FileInfo.Length; var request = WebRequest.Create(upload.Url) as HttpWebRequest; request.Accept = "*/*"; if (upload.Url.Contains("wx2")) { request.Host = "file.wx2.qq.com"; request.Headers.Add("request", "https://wx2.qq.com"); request.Referer = "https://wx2.qq.com/"; request.Headers.Add("Origin", "https://wx2.qq.com"); } else { request.Host = "file.wx.qq.com"; request.Headers.Add("request", "https://wx.qq.com"); request.Referer = "https://wx.qq.com/"; request.Headers.Add("Origin:", "https://wx.qq.com"); } request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4"); request.Headers.Add("Accept-Encoding", "gzip,deflate"); request.ContentType = "multipart/form-data; boundary=----WebKitFormBoundaryLcLGZdwXomd67JVF"; request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; request.Method = "POST"; request.ServicePoint.Expect100Continue = false; string postbody = "------WebKitFormBoundaryLcLGZdwXomd67JVF\r\n"; postbody += "Content-Disposition: form-data; name=\"id\"\r\n\r\n"; postbody += "WU_FILE_2\r\n"; postbody += "------WebKitFormBoundaryLcLGZdwXomd67JVF\r\n"; postbody += "Content-Disposition: form-data; name=\"name\"\r\n\r\n"; postbody += filename + "\r\n"; postbody += "------WebKitFormBoundaryLcLGZdwXomd67JVF\r\n"; postbody += "Content-Disposition: form-data; name=\"type\"\r\n\r\n"; postbody += "" + upload.type + "\r\n"; postbody += "------WebKitFormBoundaryLcLGZdwXomd67JVF\r\n"; postbody += "Content-Disposition: form-data; name=\"lastModifiedDate\"\r\n\r\n"; postbody += upload.FileInfo.LastWriteTime.ToString("ddd MMM dd yyyy HH:mm:ss", CultureInfo.CreateSpecificCulture("en-US")) + " GMT+0800 (中国标准时间)" + "\r\n"; postbody += "------WebKitFormBoundaryLcLGZdwXomd67JVF\r\n"; postbody += "Content-Disposition: form-data; name=\"size\"\r\n\r\n"; postbody += filesize + "\r\n"; postbody += "------WebKitFormBoundaryLcLGZdwXomd67JVF\r\n"; postbody += "Content-Disposition: form-data; name=\"mediatype\"\r\n\r\n"; postbody += "" + upload.mediatype + "\r\n"; postbody += "------WebKitFormBoundaryLcLGZdwXomd67JVF\r\n"; postbody += "Content-Disposition: form-data; name=\"uploadmediarequest\"\r\n\r\n"; postbody += upload.uploadmediarequest + "\r\n"; postbody += "------WebKitFormBoundaryLcLGZdwXomd67JVF\r\n"; postbody += "Content-Disposition: form-data; name=\"webwx_data_ticket\"\r\n\r\n"; postbody += webwx_data_ticket.Value + "\r\n"; postbody += "------WebKitFormBoundaryLcLGZdwXomd67JVF\r\n"; postbody += "Content-Disposition: form-data; name=\"pass_ticket\"\r\n\r\n"; postbody += "undefined\r\n"; postbody += "------WebKitFormBoundaryLcLGZdwXomd67JVF\r\n"; postbody += "Content-Disposition: form-data; name=\"filename\"; filename=\"" + filename + "\"\r\n"; postbody += "Content-Type: " + upload.ContentType + "\r\n\r\n"; FileStream fs = upload.FileInfo.OpenRead(); try { var sw = new StreamWriter(request.GetRequestStream()); sw.Write(postbody); sw.Flush(); //文件数据不能读为string,要直接读byte byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0) { sw.BaseStream.Write(buffer, 0, bytesRead); } sw.Write("\r\n------WebKitFormBoundaryLcLGZdwXomd67JVF--\r\n"); sw.Flush(); sw.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream response_stream = response.GetResponseStream(); int count = (int)response.ContentLength; int offset = 0; byte[] buf = new byte[count]; while (count > 0) //读取返回数据 { int n = response_stream.Read(buf, offset, count); if (n == 0) { break; } count -= n; offset += n; } response_stream.Close(); return(buf); } catch { return(null); } finally { fs.Close(); } }