Example #1
0
        /// <summary>
        /// 登陆微信公众平台函数
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="pass">密码</param>
        /// <returns></returns>
        public static bool GoToLogin(string name, string pass)//登陆微信公众平台函数
        {
            bool   result   = false;
            string password = SecurityUtility.Md5EncryptStr32(pass).ToUpper();
            string padata   = "username="******"&pwd=" + password + "&imgcode=&f=json";
            string url      = "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN ";//请求登录的URL

            try
            {
                CookieContainer cc          = new CookieContainer();                  //接收缓存
                byte[]          byteArray   = Encoding.UTF8.GetBytes(padata);         // 转化
                HttpWebRequest  webRequest2 = (HttpWebRequest)WebRequest.Create(url); //新建一个WebRequest对象用来请求或者响应url
                webRequest2.CookieContainer = cc;                                     //保存cookie
                webRequest2.Method          = "POST";                                 //请求方式是POST
                webRequest2.ContentType     = "application/x-www-form-urlencoded";    //请求的内容格式为application/x-www-form-urlencoded
                webRequest2.ContentLength   = byteArray.Length;
                webRequest2.Referer         = "https://mp.weixin.qq.com/";
                Stream newStream = webRequest2.GetRequestStream();  //返回用于将数据写入 Internet 资源的 Stream。
                // Send the data.
                newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
                newStream.Close();
                HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
                StreamReader    sr2       = new StreamReader(response2.GetResponseStream(), Encoding.Default);
                string          text2     = sr2.ReadToEnd();
                //此处用到了newtonsoft来序列化
                LoginInfo.Err = text2;
                WeiXinRetInfo retinfo = Newtonsoft.Json.JsonConvert.DeserializeObject <WeiXinRetInfo>(text2);//新版返回JSon更改
                string        token   = string.Empty;
                if (retinfo.ErrMsg.Length > 0)
                {
                    if (retinfo.ErrMsg.Contains("ok"))
                    {
                        token = retinfo.ErrMsg.Split(new char[] { '&' })[2].Split(new char[] { '=' })[1].ToString();//取得令牌
                        LoginInfo.LoginCookie = cc;
                        LoginInfo.CreateDate  = DateTime.Now;
                        LoginInfo.Token       = token;
                        result = true;
                    }
                    else
                    {
                        result = false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.StackTrace);
            }
            return(result);
        }
Example #2
0
    public static bool ExecLogin(string name, string pass)
    {
        bool   result   = false;
        string password = GetMd5Str32(pass).ToUpper();
        string padata   = "username="******"&pwd=" + password + "&imgcode=&f=json";
        string url      = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN ";//请求登录的URL

        try
        {
            CookieContainer cc          = new CookieContainer();                  //接收缓存
            byte[]          byteArray   = Encoding.UTF8.GetBytes(padata);         // 转化
            HttpWebRequest  webRequest2 = (HttpWebRequest)WebRequest.Create(url); //新建一个WebRequest对象用来请求或者响应url
            webRequest2.Referer         = "https://mp.weixin.qq.com/cgi-bin/loginpage?lang=zh_CN&t=wxm2-login";
            webRequest2.CookieContainer = cc;                                     //保存cookie
            webRequest2.Method          = "POST";                                 //请求方式是POST
            webRequest2.ContentType     = "application/x-www-form-urlencoded";    //请求的内容格式为application/x-www-form-urlencoded
            webRequest2.ContentLength   = byteArray.Length;

            Stream newStream = webRequest2.GetRequestStream();  //返回用于将数据写入 Internet 资源的 Stream。
            // Send the data.
            newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
            newStream.Close();
            HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
            StreamReader    sr2       = new StreamReader(response2.GetResponseStream(), Encoding.Default);
            string          text2     = sr2.ReadToEnd();

            //此处用到了newtonsoft来序列化
            WeiXinRetInfo retinfo = Newtonsoft.Json.JsonConvert.DeserializeObject <WeiXinRetInfo>(text2);
            string        token   = string.Empty;
            if (retinfo.base_resp.ret == 0)
            {
                token = retinfo.redirect_url.Split(new char[] { '&' })[2].Split(new char[] { '=' })[1].ToString();//取得令牌
                Caching.Set("LoginCookie", cc, 300);
                Caching.Set("CreateDate", DateTime.Now, 300);
                Caching.Set("Token", token, 300);
                LoginInfo.LoginCookie = cc;
                LoginInfo.CreateDate  = DateTime.Now;
                LoginInfo.Token       = token;
                result = true;
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.StackTrace);
        }
        return(result);
    }