示例#1
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");

            if (context.Request["filenameA"] != null)
            {
                filenameA = context.Request["filenameA"].ToString();
            }
            var file = context.Request.Files[0];

            HttpPostedFile files = context.Request.Files[0];

            string newFile = DateTime.Now.ToString("yyyyMMddHHmmss");
            //Access_token model = new Access_token();
            //model = pub.Check_Token();
            string PUBLICIF = "http://wxwh.oysd.cn/SaveWeChatData.ashx";

            WX.WX_Token wxproc = new WX.WX_Token();
            string      token  = wxproc.getToken(PUBLICIF);
            Token1      oToken = new Token1();

            oToken = JsonConvert.DeserializeObject <Token1>(token);


            string type = "voice";
            string url  = string.Format("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", oToken.result, type.ToString());


            //这里是个坑,临时文件不能上传,需要保存到某个路径下再上传----------

            string path = "G://bs//CRM-BS//BSProject//新版2017//ChangYi//image//" + files.FileName;

            //服务器上的UpLoadFile文件夹必须有读写权限      
            files.SaveAs(path);

            string filename = System.Web.HttpContext.Current.Server.MapPath("/image/" + files.FileName);

            //string filename = filenameA;



            string  json = HttpUploadFile(url, filename);
            JObject jb   = (JObject)JsonConvert.DeserializeObject(json);//这里就能知道返回正确的消息了下面是个人的逻辑我就没写
        }
示例#2
0
        public void ProcessRequest(HttpContext context)
        {
            string msg      = "";
            string PUBLICIF = "";
            string postData = "";
            string cardjson = "";

            if (context.Request["PUBLICIF"] != null)
            {
                PUBLICIF = context.Request["PUBLICIF"].ToString();
            }
            if (context.Request["postData"] != null)
            {
                postData = context.Request["postData"].ToString();
            }
            PUBLICIF = context.Request["PUBLICIF"].ToString();
            context.Response.ContentType = "text/plain";
            context.Response.ContentType = "text/html; charset=utf-8";
            //返回提示
            ErrMsg NoteObject = new ErrMsg();
            string outpost    = string.Empty;
            //取出file上传文件
            var  MediaUrl = "";
            card ObjVideo = JsonConvert.DeserializeObject <card>(postData);

            WX.WX_Token wxproc = new WX.WX_Token();
            string      token  = wxproc.getToken(PUBLICIF);
            Token1      oToken = new Token1();

            oToken = JsonConvert.DeserializeObject <Token1>(token);
            switch (context.Request["func"])
            {
            case "QRCODE":
                //MediaUrl = "https://api.weixin.qq.com/card/qrcode/create?access_token=";
                MediaUrl = String.Format("https://api.weixin.qq.com/card/qrcode/create?access_token={0}", oToken.result);

                QR_CARD cardinfo = new QR_CARD();
                cardinfo.action_info.card.card_id = ObjVideo.card_id;
                cardjson = JsonConvert.SerializeObject(cardinfo);

                break;

            case "CONTENT":
                MediaUrl = String.Format("https://api.weixin.qq.com/card/mpnews/gethtml?access_token={0}", oToken.result);
                cardjson = JsonConvert.SerializeObject(ObjVideo);

                break;

            default:
                break;
            }
            bool isok = SendHttpPostRequest(out msg, MediaUrl, cardjson, out outpost);

            WriteToLog("{" + MediaUrl + oToken.result + "}\r\nresult:" + outpost);
            WX_Mediaid oCard = new WX_Mediaid();

            oCard = JsonConvert.DeserializeObject <WX_Mediaid>(outpost);
            //tp_mediaid = JsonConvert.DeserializeObject<WX_Mediaid>(tp_media);

            context.Response.Write(JsonConvert.SerializeObject(oCard));
        }
        public void ProcessRequest(HttpContext context)
        {
            string PUBLICIF = "";//取人员商户代码

            if (context.Request["PUBLICIF"] != null)
            {
                PUBLICIF = context.Request["PUBLICIF"].ToString();
            }
            PUBLICIF = context.Request["PUBLICIF"].ToString();
            context.Response.ContentType = "text/plain";
            context.Response.ContentType = "text/html; charset=utf-8";
            //返回提示
            ErrMsg NoteObject = new ErrMsg();
            //取出file上传文件
            HttpPostedFile file     = context.Request.Files[0];
            string         fileType = context.Request.QueryString["type"];
            //取本地路径,先将文件上传到服务器(程序服务器)
            string mapPath = context.Server.MapPath("~");
            string path    = mapPath + "\\WeiXin\\";

            if (file != null && file.ContentLength > 0)
            {
                int    imagesKindInx = file.FileName.LastIndexOf(".");
                string fileNewName   = ConvertDateTimeInt(DateTime.Now) + file.FileName.Substring(imagesKindInx, file.FileName.Length - imagesKindInx);
                string savePath      = path + fileNewName;
                //本地目录不存在创建
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                //本地命名重复存在删除
                if (File.Exists(savePath))
                {
                    File.Delete(savePath);
                }
                //保存文件在本地
                file.SaveAs(savePath);

                WX.WX_Token wxproc = new WX.WX_Token();
                string      token  = wxproc.getToken(PUBLICIF);
                Token1      oToken = new Token1();
                oToken = JsonConvert.DeserializeObject <Token1>(token);

                WebClient c = new WebClient();


                byte[] result   = c.UploadFile(new Uri(String.Format("http://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={0}", oToken.result)), savePath);
                string tp_media = Encoding.Default.GetString(result);



                WriteToLog("url:http://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={" + oToken.result + "}\r\nresult:" + tp_media + "savePath:" + savePath);

                WX_Mediaid tp_mediaid = new WX_Mediaid();
                tp_mediaid = JsonConvert.DeserializeObject <WX_Mediaid>(tp_media);
                if (tp_mediaid.errcode != 0)
                {
                    NoteObject.errCode    = 2;
                    NoteObject.errMessage = "上传微信服务器:" + tp_mediaid.errmsg;
                }
                else
                {
                    NoteObject.result = tp_mediaid.url;
                }
                context.Response.Write(JsonConvert.SerializeObject(NoteObject));
            }
        }
示例#4
0
        public void ProcessRequest(HttpContext context)
        {
            string PUBLICIF = "";
            string postData = "";

            if (context.Request["PUBLICIF"] != null)
            {
                PUBLICIF = context.Request["PUBLICIF"].ToString();
            }
            if (context.Request["postData"] != null)
            {
                postData = context.Request["postData"].ToString();
            }
            PUBLICIF = context.Request["PUBLICIF"].ToString();
            context.Response.ContentType = "text/plain";
            context.Response.ContentType = "text/html; charset=utf-8";
            //返回提示
            ErrMsg NoteObject = new ErrMsg();
            //取出file上传文件
            HttpPostedFile file     = context.Request.Files[0];
            string         fileType = context.Request.QueryString["type"];
            //取本地路径,先将文件上传到服务器(程序服务器)
            string mapPath = context.Server.MapPath("~");
            string path    = mapPath + "\\WeiXin\\";

            if (file != null && file.ContentLength > 0)
            {
                int    imagesKindInx = file.FileName.LastIndexOf(".");
                string fileNewName   = ConvertDateTimeInt(DateTime.Now) + file.FileName.Substring(imagesKindInx, file.FileName.Length - imagesKindInx);
                string savePath      = path + fileNewName;
                //本地目录不存在创建
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                //本地命名重复存在删除
                if (File.Exists(savePath))
                {
                    File.Delete(savePath);
                }
                //保存文件在本地
                file.SaveAs(savePath);

                //将文件转化为字节
                FileStream fs        = new FileStream(savePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                byte[]     fileBytes = new byte[fs.Length];
                fs.Read(fileBytes, 0, fileBytes.Length);
                fs.Close();
                fs.Dispose();

                //获取token
                WX.WX_Token wxproc = new WX.WX_Token();
                string      token  = wxproc.getToken(PUBLICIF);
                Token1      oToken = new Token1();
                oToken = JsonConvert.DeserializeObject <Token1>(token);

                WebClient wc = new WebClient();
                wc.Encoding = ASCIIEncoding.UTF8;
                var    MediaUrl = "";
                string tp_media = "";
                if (fileType == "newsimg")  //图文图片接口 只返回图片地址
                {
                    MediaUrl = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=";
                    byte[] result = wc.UploadFile(new Uri(String.Format(MediaUrl + "{0}", oToken.result)), savePath);
                    tp_media = Encoding.Default.GetString(result);
                }
                else //image voice video thumb
                {
                    MediaUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=";
                    string newUri = Convert.ToString(new Uri(String.Format(MediaUrl + "{0}&type={1}", oToken.result, fileType)));

                    //设置表单数据格式
                    HttpRequestClient httpRequestClient = new HttpRequestClient();
                    if (fileType == "video")  //上传视频需要传title、description
                    {
                        VideoInfo ObjVideo = JsonConvert.DeserializeObject <VideoInfo>(postData);
                        httpRequestClient.SetFieldValue("title", ObjVideo.title);                              //发送数据
                        httpRequestClient.SetFieldValue("introduction", ObjVideo.introduction);                //发送数据
                    }
                    httpRequestClient.SetFieldValue("media", savePath, "application/octet-stream", fileBytes); //发送文件数据
                    string responseText = string.Empty;
                    httpRequestClient.Upload(newUri, out responseText);                                        //请求  responseText是返回结果
                    tp_media = responseText;
                }


                WX_Mediaid tp_mediaid = new WX_Mediaid();
                tp_mediaid = JsonConvert.DeserializeObject <WX_Mediaid>(tp_media);
                if (tp_mediaid.errcode != 0)
                {
                    NoteObject.errCode    = 2;
                    NoteObject.errMessage = "上传微信服务器:" + tp_mediaid.errmsg;
                    context.Response.Write(JsonConvert.SerializeObject(NoteObject));
                }
                else
                {
                    if (fileType == "newsimg")
                    {
                        ImgMsg imgObject = new ImgMsg();
                        imgObject.error = 0;
                        imgObject.url   = tp_mediaid.url;
                        context.Response.Write(JsonConvert.SerializeObject(imgObject));
                        // NoteObject.result = tp_mediaid.url;
                        //context.Response.Write(NoteObject.result);
                    }
                    else
                    {
                        NoteObject.result = tp_mediaid.media_id;
                        context.Response.Write(JsonConvert.SerializeObject(NoteObject));
                    }
                }
                //context.Response.Write(JsonConvert.SerializeObject(NoteObject));
            }
        }