码迷,mamicode.com
首页 > Windows程序 > 详细

请教淘宝API sign签名问题,望高人指点!谢谢!

时间:2014-06-03 01:47:55      阅读:980      评论:0      收藏:0      [点我收藏+]

标签:淘宝   api   

 


   对淘宝API sign签名老计算不对

代码如下:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //正确的sign
            //http://gw.api.taobao.com/router/rest?sign=70950D9D8BEC7BF55BA572902733F261×tamp=2014-05-28+00%3A04%3A02&v=2.0&app_key=21792839&method=taobao.time.get&partner_id=top-apitools&format=json
            Dictionary<string, string> param = new Dictionary<string, string>();

            param["app_key"] = "21792839";
            param["format"] = "json";
            param["method"] = "taobao.time.get";
            param["partner_id"] = "top-apitools";
            param["timestamp"] = "2014-05-28 00:04:02";
            param["v"] = "2.0";

            string secret = "c22cd49550a5fac8a775893a34a0391c";
            string x = getMD5String(param, secret);
            textBox1.Text = x;
        }
        public static String getMD5String(Dictionary<string, string> param, String str)
        {
            String gets = getBeforeSign(param, str);
            Console.WriteLine(gets);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash((new UTF8Encoding()).GetBytes(gets));
            //result = md5.ComputeHash(result);
            return byteArrayToHexString(result);
        }

        private static String byteArrayToHexString(byte[] b)
        {
            StringBuilder resultSb = new StringBuilder();
            string stmp;
            for (int i = 0; i < b.Length; i++)
            {
                int md = (b[i] & 0XFF);
                stmp = "" + md.ToString("X"); ;
                if (stmp.Length == 1)
                    resultSb.Append("0").Append(stmp);
                else
                    resultSb.Append(stmp);
            }
            return resultSb.ToString().ToUpper();
        }
        private static String getBeforeSign(Dictionary<String, String> param, String orgin)
        {
            String indexs = orgin;
            if (param == null)
                return null;
            foreach (KeyValuePair<String, String> var in param)
            {
                String name = (String)var.Key.ToString();
                orgin += name;
                orgin += param[name];
            }

            orgin = orgin + indexs;
            return orgin;
        }

    }
}


运行结果:

bubuko.com,布布扣

848457EDB79276ACDC1B1E636CDC5757

 

正确结果:

70950D9D8BEC7BF55BA572902733F261

请问是什么问题?

请教淘宝API sign签名问题,望高人指点!谢谢!,布布扣,bubuko.com

请教淘宝API sign签名问题,望高人指点!谢谢!

标签:淘宝   api   

原文地址:http://blog.csdn.net/qwestw/article/details/27564227

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!