public void Uploadmedia(string fromUserName, string toUserName, string id, string mime_type, int uploadType, int mediaType, byte[] buffer, string fileName, string pass_ticket, BaseRequest baseReq, Action <UploadmediaResponse> CallBack)
        {
            //TODO 暂未实现图片上传
            ReportForWeb(() => {
                UploadmediaRequest req = new UploadmediaRequest
                {
                    UploadType    = uploadType,
                    BaseRequest   = baseReq,
                    ClientMediaId = GetR(),
                    TotalLen      = buffer.Length,
                    StartPos      = 0,
                    DataLen       = buffer.Length,
                    MediaType     = mediaType,
                    FromUserName  = fromUserName,
                    ToUserName    = toUserName,

                    FileMd5 = Util.GetMD5(buffer)
                };

                string url = "https://file.wx2.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json";
                http.GetHtml(url, "option");

                string requestJson = JsonConvert.SerializeObject(req);
                string mt          = "doc";
                if (mime_type.StartsWith("image/"))
                {
                    mt = "pic";
                }
                //var dataTicketCookie = GetCookie("webwx_data_ticket");

                //	var dataContent = new MultipartFormDataContent
                //{
                //	{ new StringContent(id), "id" },
                //	{ new StringContent(fileName), "name" },
                //	{ new StringContent(mime_type), "type" },
                //	{ new StringContent("2018/2/23 下午11:23:33"), "lastModifiedDate" },
                //	{ new StringContent(buffer.Length.ToString()), "size" },
                //	{ new StringContent(mt), "mediatype" },
                //	{ new StringContent(requestJson), "uploadmediarequest" },
                //	{ new StringContent(dataTicketCookie.Value), "webwx_data_ticket" },
                //	{ new StringContent("undefined"), "pass_ticket" },
                //	{ new ByteArrayContent(buffer) },
                //	{ new StringContent( fileName + "\r\n Content - Type: " + mime_type) ,"filename"}
                //};

                //try
                //{
                //	var response = mHttpClient.PostAsync(url, dataContent).Result;
                //	string repJsonStr = response.Content.ReadAsStringAsync().Result;
                //	var rep = JsonConvert.DeserializeObject<UploadmediaResponse>(repJsonStr);
                //	return rep;
                //}
                //catch (Exception ex)
                //{
                //	Console.WriteLine("uploadmedia()" + ex.Message);
                //	return null;
                //}
            });
        }
Ejemplo n.º 2
0
        public UploadmediaResponse Uploadmedia(string fromUserName, string toUserName, string id, string mime_type, int uploadType, int mediaType, byte[] buffer, string fileName, string pass_ticket, BaseRequest baseReq)
        {
            ReportForWeb();
            UploadmediaRequest req = new UploadmediaRequest
            {
                UploadType    = uploadType,
                BaseRequest   = baseReq,
                ClientMediaId = GetR(),
                TotalLen      = buffer.Length,
                StartPos      = 0,
                DataLen       = buffer.Length,
                MediaType     = mediaType,
                FromUserName  = fromUserName,
                ToUserName    = toUserName,

                FileMd5 = Util.GetMD5(buffer)
            };

            string url = "https://file.wx2.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json";

            mHttpClient.SendAsync(new HttpRequestMessage(HttpMethod.Options, url));

            string requestJson = JsonConvert.SerializeObject(req);
            string mt          = "doc";

            if (mime_type.StartsWith("image/"))
            {
                mt = "pic";
            }
            var dataTicketCookie = GetCookie("webwx_data_ticket");

            var dataContent = new MultipartFormDataContent
            {
                { new StringContent(id), "id" },
                { new StringContent(fileName), "name" },
                { new StringContent(mime_type), "type" },
                { new StringContent("2018/2/23 下午11:23:33"), "lastModifiedDate" },
                { new StringContent(buffer.Length.ToString()), "size" },
                { new StringContent(mt), "mediatype" },
                { new StringContent(requestJson), "uploadmediarequest" },
                { new StringContent(dataTicketCookie.Value), "webwx_data_ticket" },
                { new StringContent("undefined"), "pass_ticket" },
                { new ByteArrayContent(buffer) },
                { new StringContent(fileName + "\r\n Content - Type: " + mime_type), "filename" }
            };

            try
            {
                var    response   = mHttpClient.PostAsync(url, dataContent).Result;
                string repJsonStr = response.Content.ReadAsStringAsync().Result;
                var    rep        = JsonConvert.DeserializeObject <UploadmediaResponse>(repJsonStr);
                return(rep);
            }
            catch (Exception ex) {
                Console.WriteLine("uploadmedia()" + ex.Message);
                return(null);
            }
        }
Ejemplo n.º 3
0
        public UploadmediaResponse Uploadmedia(string fromUserName, string toUserName, string id, string mime_type, int uploadType, int mediaType, FileInfo file, string pass_ticket, BaseRequest baseReq)
        {
            UploadmediaRequest req = new UploadmediaRequest();

            req.BaseRequest   = baseReq;
            req.ClientMediaId = getR();
            req.DataLen       = file.Length;
            req.StartPos      = 0;
            req.TotalLen      = file.Length;
            req.MediaType     = mediaType;
            req.FromUserName  = fromUserName;
            req.ToUserName    = toUserName;
            req.UploadType    = uploadType;
            req.FileMd5       = Util.GetMD5HashFromFile(file.FullName);

            string url = "https://file.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json";

            if (root_uri.Contains("wx2.qq.com"))
            {
                url = "https://file.wx2.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json";
            }

            string requestJson = JsonConvert.SerializeObject(req);
            string mt          = "doc";

            if (mime_type.StartsWith("image/"))
            {
                mt = "pic";
            }
            var dataTicketCookie = GetCookie("webwx_data_ticket");

            try
            {
                Dictionary <string, string> map = new Dictionary <string, string>();
                map.Add("id", id);
                map.Add("name", file.Name);
                map.Add("type", mime_type);
                map.Add("lastModifiedDate", DateTime.Now.ToString());
                map.Add("size", file.Length + "");
                map.Add("mediatype", mt);
                map.Add("uploadmediarequest", requestJson);
                map.Add("pass_ticket", pass_ticket);
                map.Add("webwx_data_ticket", dataTicketCookie.Value);

                string repJsonStr = PostFile(url, file, map, mime_type);

                //var response = mHttpClient.PostAsync(url, dataContent).Result;
                //string repJsonStr = response.Content.ReadAsStringAsync().Result;
                var rep = JsonConvert.DeserializeObject <UploadmediaResponse>(repJsonStr);
                return(rep);
            }
            catch
            {
                InitHttpClient();
                return(null);
            }
        }
Ejemplo n.º 4
0
        public UploadmediaResponse Uploadmedia(string fromUserName, string toUserName, string id, string mime_type, int uploadType, int mediaType, byte[] buffer, string fileName, string pass_ticket, BaseRequest baseReq)
        {
            UploadmediaRequest req = new UploadmediaRequest();

            req.BaseRequest   = baseReq;
            req.ClientMediaId = getR();
            req.DataLen       = buffer.Length;
            req.StartPos      = 0;
            req.TotalLen      = buffer.Length;
            req.MediaType     = mediaType;
            req.FromUserName  = fromUserName;
            req.ToUserName    = toUserName;
            req.UploadType    = uploadType;
            req.FileMd5       = Util.getMD5(buffer);

            string url         = "https://file.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json";
            string requestJson = JsonConvert.SerializeObject(req);
            string mt          = "doc";

            if (mime_type.StartsWith("image/"))
            {
                mt = "pic";
            }
            var dataTicketCookie = GetCookie("webwx_data_ticket");

            var dataContent = new MultipartFormDataContent();

            dataContent.Add(new StringContent(id), "id");
            dataContent.Add(new StringContent(fileName), "name");
            dataContent.Add(new StringContent(mime_type), "type");
            dataContent.Add(new StringContent("Thu Mar 17 2016 14:35:28 GMT+0800 (中国标准时间)"), "lastModifiedDate");
            dataContent.Add(new StringContent(buffer.Length.ToString()), "size");
            dataContent.Add(new StringContent(mt), "mediatype");
            dataContent.Add(new StringContent(requestJson), "uploadmediarequest");
            dataContent.Add(new StringContent(dataTicketCookie.Value), "webwx_data_ticket");
            dataContent.Add(new StringContent(pass_ticket), "pass_ticket");
            dataContent.Add(new ByteArrayContent(buffer), "filename", fileName + "\r\n Content - Type: " + mime_type);

            try
            {
                var    response   = mHttpClient.PostAsync(url, dataContent).Result;
                string repJsonStr = response.Content.ReadAsStringAsync().Result;
                var    rep        = JsonConvert.DeserializeObject <UploadmediaResponse>(repJsonStr);
                return(rep);
            }
            catch {
                InitHttpClient();
                return(null);
            }
        }
Ejemplo n.º 5
0
        public UploadmediaResponse Uploadmedia(string fromUserName, string toUserName, string id, string mime_type, int uploadType, int mediaType, byte[] buffer, string fileName, string pass_ticket, BaseRequest baseReq)
        {
            UploadmediaRequest req = new UploadmediaRequest();

            req.BaseRequest   = baseReq;
            req.ClientMediaId = getR();
            req.DataLen       = buffer.Length;
            req.StartPos      = 0;
            req.TotalLen      = buffer.Length;
            req.MediaType     = mediaType;
            req.FromUserName  = fromUserName;
            req.ToUserName    = toUserName;
            req.UploadType    = uploadType;
            req.FileMd5       = Util.getMD5(buffer);

            string url         = "https://file.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json";
            string requestJson = JsonConvert.SerializeObject(req);
            string mt          = "doc";

            if (mime_type.StartsWith("image/"))
            {
                mt = "pic";
            }
            var dataTicketCookie = GetCookie("webwx_data_ticket");

            NameValueCollection data = new NameValueCollection();

            data.Add("id", id);
            data.Add("name", fileName);
            data.Add("type", mime_type);
            data.Add("lastModifiedDate", "Thu Mar 17 2016 14:35:28 GMT+0800 (中国标准时间)");
            data.Add("size", buffer.Length.ToString());
            data.Add("mediatype", mt);
            data.Add("uploadmediarequest", requestJson);
            data.Add("webwx_data_ticket", dataTicketCookie.Value);
            data.Add("pass_ticket", pass_ticket);
            var repJsonBuf = UploadFile(url, buffer, fileName, mime_type, data, Encoding.UTF8);
            var repJsonStr = Encoding.UTF8.GetString(repJsonBuf);
            var rep        = JsonConvert.DeserializeObject <UploadmediaResponse>(repJsonStr);

            return(rep);
        }