Exemplo n.º 1
0
    public static string sPostUrl = "http://www.fandian99.com/PrintInterface/API/response";//pos地址

    public Boolean Login(string _userid, string _password)
    {
        TransFormat objformat;

        //构建poscontent
        Hashtable ht = new Hashtable();

        ht.Add("user", _userid);
        ht.Add("password", _password);
        ht.Add("action", "check");
        ht.Add("module", "login");
        Des coder = new Des();
        //时间戳
        long myTS = 0L;
        //加密
        string connentEnCode = coder.EnCode(HashtableToJson(ht), out myTS);

        objformat.TS          = myTS + "";
        objformat.PostContent = connentEnCode;
        objformat.Post_extra  = "";

        string sRequesturl = PBuildRequestpara(objformat);

        //pos数据
Retry:
        HttpWebRequest request = (HttpWebRequest)WebRequest.CreateDefault(
            new Uri(sPostUrl));

        request.UserAgent = "fandian99 print tools";//协议标头
        request.Timeout   = 5000;
        byte[] readbytes = Encoding.ASCII.GetBytes(sRequesturl);
        request.Method        = "POST";
        request.ContentType   = "application/x-www-form-urlencoded";
        request.ContentLength = readbytes.Length;
        string responseData = "";

        try
        {
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(readbytes, 0, readbytes.Length);
                reqStream.Close();
            }
            DataSet ds = new DataSet();
            string  token, iID, iLoginID;
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    responseData = reader.ReadToEnd().ToString();
                    //处理函数
                    //去除unicode字符
                    Byte[]   bys  = Encoding.UTF8.GetBytes(responseData);
                    Encoding ed   = new UTF8Encoding(false);
                    Byte[]   bys2 = Encoding.Convert(Encoding.UTF8, ed, bys);
                    string   str  = ed.GetString(bys2);
                    using (StringReader xmlSR = new StringReader(str))
                    {
                        ds.ReadXml(xmlSR, XmlReadMode.InferTypedSchema);
                        if (ds.Tables.Count > 0)
                        {
                            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                            {
                                token = ds.Tables[0].Rows[0]["TOKEN"].ToString();
                                //用户ID
                                iID = ds.Tables[0].Rows[0]["userID"].ToString();
                                //收款员ID/登录人ID
                                iLoginID = ds.Tables[0].Rows[0]["LoginID"].ToString();
                                return(true);
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            if (System.Windows.Forms.MessageBox.Show("连接服务器失败,是否重试", "消息", System.Windows.Forms.MessageBoxButtons.RetryCancel) == System.Windows.Forms.DialogResult.Retry)
            {
                goto Retry;
            }
        }
        finally
        {
        }
        return(false);
    }