Exemplo n.º 1
0
        public void DoRequest()
        {
            var methord = cbMethord.SelectedItem.ToString();

            if (methord.Equals("post", StringComparison.OrdinalIgnoreCase))
            {
                var rslt = HttpHelper.Instance.PostDataTo(tbUrl.Text,
                                                          tbRequestBody.Text,
                                                          new Dictionary <string, string>()
                {
                    { tbIdKey.Text, tbAppId.Text },
                    { tbAppSecretyKey.Text, PercentEncoding.GenerateSignature(tbUrl.Text, "post", tbRequestBody.Text, tbAppSecurity.Text) }
                }
                                                          );
                tbResult.Text = rslt;
            }
            else
            {
                var rslt = HttpHelper.Instance.GetResponseFrom(tbUrl.Text,
                                                               new Dictionary <string, string>()
                {
                    { tbIdKey.Text, tbAppId.Text },
                    { tbAppSecretyKey.Text, PercentEncoding.GenerateSignature(tbUrl.Text, "get", "", tbAppSecurity.Text) }
                }
                                                               );
                tbResult.Text = rslt;
            }
        }
Exemplo n.º 2
0
        public void UploadFile()
        {
            using (var client = new HttpClient())
            {
                var fileName = tbFilePath.Text;
                var strUrl   = tbUrl.Text;
                client.DefaultHeaders.Add(tbIdKey.Text, tbAppId.Text);
                var sign = PercentEncoding.GenerateSignature(tbUrl.Text, "post", GetFileBase64(), tbAppSecurity.Text);
                client.DefaultHeaders.Add(tbAppSecretyKey.Text, sign);

                var content = HttpContent.Create(File.OpenRead(fileName));
                try
                {
                    var response = client.Post(strUrl, content);
                    response.EnsureStatusIsSuccessful();
                    var readResult = response.Content.ReadAsString();
                    tbResult.Text = readResult;
                }
                catch (Exception exp)
                {
                    tbResult.Text = exp.Message;
                }
                finally
                {
                    content.Dispose();
                }
            }
        }
Exemplo n.º 3
0
        public static string GenerateSignature(string url, string method, string requestBody, string appSecret, StringBuilder log = null)
        {
            List <string> combined = new List <string>();

            // request method
            combined.Add(method.ToUpper());

            Uri uri = new Uri(url);

            // scheme
            combined.Add(uri.Scheme.ToLower());
            // host
            combined.Add(uri.Host.ToLower());
            // port
            combined.Add(uri.Port.ToString());
            // path
            string path = uri.AbsolutePath.ToLower();

            path = path.Replace("\\", "/");
            if (path.EndsWith("/"))
            {
                path = path.Substring(0, path.Length - 1);
            }
            combined.Add(PercentEncoding.Encode(path));

            // query string
            string q = (uri.Query ?? "").Trim();

            if (q.Length > 0)
            {
                if (q.StartsWith("?"))
                {
                    q = q.Substring(1);
                }
                string[] itemStrs = q.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                List <KeyValuePair <string, string> > items = new List <KeyValuePair <string, string> >();
                foreach (string itemStr in itemStrs)
                {
                    if (itemStr.Trim().Length == 0)
                    {
                        continue;
                    }
                    string key = "", value = "";

                    int index = itemStr.IndexOf("=");
                    if (index <= 0) // = is missing or key is missing, ignore
                    {
                        continue;
                    }
                    else
                    {
                        key   = HttpUtility.UrlDecode(itemStr.Substring(0, index)).Trim().ToLower();
                        value = HttpUtility.UrlDecode(itemStr.Substring(index + 1)).Trim();
                        items.Add(new KeyValuePair <string, string>(key, value));
                    }
                }

                // query
                combined.Add(String.Join("&",
                                         items.OrderBy(t => t.Key).Select(t => String.Format("{0}={1}", PercentEncoding.Encode(t.Key), PercentEncoding.Encode(t.Value))).ToArray()));
            }
            else
            {
                combined.Add("");
            }

            // body
            combined.Add(PercentEncoding.Encode(requestBody ?? ""));
            // salt
            combined.Add(appSecret);

            string baseString = String.Join("|", combined.ToArray());

            if (log != null)
            {
                log.AppendLine("Base String: " + baseString);
            }

            System.Security.Cryptography.SHA256Managed s256 = new System.Security.Cryptography.SHA256Managed();
            byte[] buff;
            buff = s256.ComputeHash(Encoding.UTF8.GetBytes(baseString));
            s256.Clear();
            return(Convert.ToBase64String(buff));
        }
Exemplo n.º 4
0
 private void btnGenerate_Click(object sender, EventArgs e)
 {
     tbBaseString.Text = PercentEncoding.GetBaseString(tbUrl.Text, cbMethord.SelectedItem.ToString(), tbRequestBody.Text, tbAppSecret.Text);
     tbSignature.Text  = PercentEncoding.GetSign(tbBaseString.Text);
 }