Пример #1
0
        public void HandleCommand(AsyncResultCallback <object> callback)
        {
            _commandCallback = callback;

            HttpClientOptions options     = BuildHttpOptions();
            string            requestData = null;

            if ((_verb == HttpVerb.POST) || (_verb == HttpVerb.PUT))
            {
                if (_webRequest.Data != null)
                {
                    string data;
                    if (_webRequest.Data.GetType() == typeof(string))
                    {
                        data = (string)_webRequest.Data;
                        if (options.Headers.ContainsKey("Content-Type") == false)
                        {
                            options.Headers["Content-Type"] = "text/plain";
                        }
                    }
                    else
                    {
                        data = Json.Stringify(_webRequest.Data);
                        options.Headers["Content-Type"] = "application/json";
                    }

                    Buffer requestBuffer = new Buffer(data);
                    requestData = requestBuffer.ToString(Encoding.UTF8);

                    options.Headers["Content-Length"] = requestData.Length.ToString();
                }
            }

            HttpClientRequest request;

            if (_urlData.Protocol == "http:")
            {
                request = Http.Request(options, HandleResponse);
            }
            else
            {
                request = Https.Request(options, HandleResponse);
            }

            if (requestData != null)
            {
                request.Write(requestData);
            }

            request.End();
        }
Пример #2
0
        async void TestStripButtonClick(object sender, EventArgs e)
        {
//			try
//			{
//					var d = new List<dynamic>();
//			d.Add(1);
//			d.Add("");
//			d.Add("33355");
//			("http://127.0.0.1:5000" + "/api/playlist").PostJson(d.ToJsonString(), "J3ebj2iT");
//			}catch(Exception ex){
//				textBox.Text=ex.Message;
//			}
            //	await	MusicDelegate.GetUrlAsync("000z8oQD1vZWYg");
            try {
                Https.SetCertificatePolicy();
                var message = ("https://106.12.125.201" + "/api/touch").PostJson(textBox.Text.Trim(), "J3ebj2iT");
                textBox.Text += Environment.NewLine + message.FormatJson();
            } catch (Exception ex) {
                textBox.Text = ex.Message;
            }
        }
Пример #3
0
        private async static Task <string> QueryFromBaidu(string q, string from, string to)
        {
            var url   = "http://api.fanyi.baidu.com/api/trans/vip/translate";
            var appid = "20190312000276185";

            var parameters = new Dictionary <string, string>();

            parameters.Add("q", q);
            parameters.Add("from", from);
            parameters.Add("to", to);
            var salt = (DateTime.Now.Ticks / 1000).ToString();

            parameters.Add("salt", salt);
            parameters.Add("appid", appid);
            var securityKey = "sdK6QhtFE64Qm0ID_SjG";
            var sb          = new StringBuilder();

            sb.Append(appid)
            .Append(q)
            .Append(salt)
            .Append(securityKey);
            parameters.Add("sign", sb.ToString().Md5());
            if (_client == null)
            {
                _client = Https.GetHttpClient();
            }
            var json = await _client.PostWithParameters(url, parameters);

            var array = json.ToJObject().GetArray <Newtonsoft.Json.Linq.JObject>("trans_result");

            sb.Clear();
            foreach (var element in array)
            {
                sb.AppendLine(element.GetString("dst"));
            }
            return(sb.ToString());
        }
        private async void DoLogin()
        {
            string user_account  = this.user_account.Text.Trim();
            string user_password = this.user_password.Password.Trim();

            if (user_account == "" || user_password == "")
            {
                return;
            }
            this.button_login.IsEnabled = false;
            this.button_login.Content   = "Loading...";
            Config.SetValue("user_account", user_account);
            Dictionary <string, string> postData = new Dictionary <string, string>()
            {
                { "user_account", user_account },
                { "user_password", user_password },
                { "plat", "windows" },
                { "version", "10000" }
            };
            JObject jo = (JObject)await Https.PostAsync("user/login", postData);

            this.button_login.IsEnabled = true;
            this.button_login.Content   = "立即登录";
            if (jo["code"].ToString().Equals(Https.CodeSuccess))
            {
                AlertWindow alert = new AlertWindow();
                //alert.showDialog("", "登录成功");
                Https.AccessToken = (string)jo["data"]["access_token"];
                this.Close();
            }
            else
            {
                AlertWindow alert = new AlertWindow();
                alert.showDialog((string)jo["msg"], "登录失败");
            }
        }
Пример #5
0
        private void RunListenServer()
        {
            try
            {
                int       port = Convert.ToInt32(portText.Text.Trim()); //服务器port
                IPAddress ip   = IPAddress.Parse(IP.GetLocalIP());      //服务器端ip
                myListener = new TcpListener(ip, port);                 //创建TcpListener实例
                myListener.Start();                                     //start
                this.RunOnUiThread(() =>                                //this 指代的是Activity对象,RunOnUiThread 是Activity的一个成员方法  
                {
                    RefreshMessageView("请连接:" + IP.GetLocalIP() + ":" + port);
                });
                while (serverRuning)
                {
                    newClient = myListener.AcceptTcpClient();//等待客户端连接,长连接
                    //this.RunOnUiThread(() =>
                    //{
                    //    RefreshMessageView("链接成功");
                    //});

                    //while (serverRuning)
                    //{
                    //try
                    //{
                    clientStream = newClient.GetStream();    //利用TcpClient对象GetStream方法得到网络流
                    TCPHelper newtcp      = new TCPHelper();
                    byte[]    btemp       = newtcp.ReceiveByteArray(clientStream);
                    Message   mes         = new Message(btemp);
                    byte      messagetype = mes.Date_9F01.Data[0];
                    string    url         = "";
                    string    logData     = "";
                    Dictionary <string, string> log;
                    byte[] barCodeData = null;

                    switch (messagetype)
                    {
                    case 0x00:
                        logData = Encoding.Default.GetString(mes.Date_9F03.Data);
                        url     = Encoding.Default.GetString(mes.Date_9F05.Data);
                        log     = UP_SDK.SDKUtil.CoverStringToDictionary(logData, Encoding.UTF8);
                        var re_00 = Https.sendOfflineBarCodeAsync(log, url);    //脱机扫码
                        break;

                    case 0x01:
                        logData = Encoding.Default.GetString(mes.Date_9F03.Data);
                        url     = Encoding.Default.GetString(mes.Date_9F05.Data);
                        log     = UP_SDK.SDKUtil.CoverStringToDictionary(logData, Encoding.UTF8);
                        var re_01 = Https.sendBarCodeAsync(log, url);
                        break;

                    case 0x02:
                        //图片扫描
                        break;

                    default:
                        break;
                    }


                    byte[] byteArray = mes.PackageMessage(barCodeData, null, true);
                    newtcp.SendByteArray(clientStream, byteArray);
                    clientStream.Close();
                    newClient.Close();
                    newClient.Dispose();

                    // Shutdown and end connection
                }
            }
            catch (Exception e)
            {
                this.RunOnUiThread(() =>
                {
                    RefreshMessageView("异常信息" + e.Message);
                });
                if (clientStream != null)
                {
                    clientStream.Close();
                    clientStream = null;
                }
                if (myListener != null)
                {
                    myListener.Stop();
                    myListener = null;
                }
                if (!e.Message.Equals("interrupted"))
                {
                    this.RunOnUiThread(() =>
                    {
                        RefreshMessageView("异常信息" + e.Message);
                        createButton.PerformClick();
                    });
                }

                return;
            }
        }