userinfo

本文提供了一个使用C#实现的1688开放平台API调用示例,包括了如何构建请求参数、生成签名、发起HTTP请求等关键步骤,并展示了如何根据不同的API需求构造URL。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Net;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using System.Configuration;
 
 
namespace aliformerp
{
    public struct acctk
    {
        public string uid;
        public string aliId;
        public string resource_owner;
        public string memberId;
        public string expires_in;
        public string refresh_token;
        public string access_token;
    }
    public class userinfo
    {
        public static acctk acctk2;
        public static string appKey = Properties.Settings.Default.appKey;
        public static string site = Properties.Settings.Default.site;
        public static string secretkey = Properties.Settings.Default.secretkey;
        public static string redirect_uri = Properties.Settings.Default.redirect_uri;
        public static string account = Properties.Settings.Default.account;
        public static string password = Properties.Settings.Default.password;
 
        public static string sign(Dictionary<stringstringparamDicstring appSecret)
        {
            byte[] signatureKey = Encoding.UTF8.GetBytes(appSecret);
            //第一步:拼装key+value
            List<stringlist = new List<string>();
            foreach (KeyValuePair<stringstringkv in paramDic)
            {
                list.Add(kv.Key + kv.Value);
            }
            //第二步:排序
            list.Sort();
            //第三步:拼装排序后的各个字符串
            string tmp = "";
            foreach (string kvstr in list)
            {
                tmp = tmp + kvstr;
            }
            //第四步:将拼装后的字符串和app密钥一起计算签名
            //HMAC-SHA1
            HMACSHA1 hmacsha1 = new HMACSHA1(signatureKey);
            hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(tmp));
            byte[] hash = hmacsha1.Hash;
            //TO HEX
            return BitConverter.ToString(hash).Replace("-"string.Empty).ToUpper();
        }
        public static string sign(string urlPathDictionary<stringstringparamDic)
        {
            byte[] signatureKey = Encoding.UTF8.GetBytes(secretkey);//此处用自己的签名密钥
            List<stringlist = new List<string>();
            foreach (KeyValuePair<stringstringkv in paramDic)
            {
                list.Add(kv.Key + kv.Value);
            }
            list.Sort();
            string tmp = urlPath;
            foreach (string kvstr in list)
            {
                tmp = tmp + kvstr;
            }
 
            //HMAC-SHA1
            HMACSHA1 hmacsha1 = new HMACSHA1(signatureKey);
            hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(tmp));
            /*
            hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(urlPath));
            foreach (string kvstr in list)
            {
                hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(kvstr));
            }
             */
            byte[] hash = hmacsha1.Hash;
            //TO HEX
            return BitConverter.ToString(hash).Replace("-"string.Empty).ToUpper();
        }
        public static string gethttpwithuri(string lingpaigeturl2)
        {
            string gethttpwithuriout = "";
            HttpWebRequest http23 = WebRequest.Create(lingpaigeturl2as HttpWebRequest;
            // http23.ServicePoint.Expect100Continue = false;
            //http23.Referer = lingpaigeturl;
            http23.Method = "GET";
            http23.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            // http23.AllowAutoRedirect = true;
            //  http23.Credentials = System.Net.CredentialCache.DefaultCredentials;
            http23.KeepAlive = true;
            http23.AllowAutoRedirect = true;
            //  http23.ProtocolVersion = HttpVersion.Version10;
            //  http23.CookieContainer = MyCookieContainer;
            //using (Stream request2 = http23.GetRequestStream())
            //{
            //    try
            //    {
            //        // request2.Write(postData, 0, postData.Length);
            //    }
            //    catch
            //    {
            //        throw;
            //    }
            //    finally
            //    {
            //        request2.Close();
            //    }
            //}
            try
            {
                HttpWebResponse response12 = http23.GetResponse() as HttpWebResponse;
                StreamReader reader12 = new StreamReader(response12.GetResponseStream());
                // richTextBox3.Text += "你获取的uri返回是:" + reader12.ReadToEnd().ToString();
                gethttpwithuriout = reader12.ReadToEnd().ToString();
                return gethttpwithuriout;
            }
            catch (System.Exception ex)
            {
                return ex.ToString();
            }
            finally
            {
 
                ///  return gethttpwithuriout; 
            }
        }
        public static string uribuider(string apitpyeDictionary<stringstringparm1bool needaccess_token)
        {
 
            if (needaccess_token)
            {
                string oldori = "http://gw.open.1688.com/openapi/param2/1/cn.alibaba.open/" + apitpye + "/" + appKey + "?";
                string signolduri = "param2/1/cn.alibaba.open/" + apitpye + "/" + appKey;
                //  string signolduri2 = oldori + "param2/1/cn.alibaba.open/" + apitpye + "?";
                parm1.Add("access_token"acctk2.access_token);
                string signwithuri = sign(signolduriparm1);
                foreach (KeyValuePair<stringstringkv in parm1)
                {
                    oldori += HttpUtility.HtmlEncode(kv.Key) + "=" + HttpUtility.HtmlEncode(kv.Value) + "&";
                }
                oldori += "_aop_signature=" + signwithuri;
                return oldori;
            }
            else
            {
                string oldori = "http://gw.open.1688.com/openapi/param2/1/cn.alibaba.open/" + apitpye + "/" + appKey + "?";
                string signolduri = "param2/1/cn.alibaba.open/" + apitpye + "/" + appKey;
                //  string signolduri2 = oldori + "param2/1/cn.alibaba.open/" + apitpye + "?";
                string signwithuri = sign(signolduriparm1);
                foreach (KeyValuePair<stringstringkv in parm1)
                {
                    oldori += HttpUtility.HtmlEncode(kv.Key) + "=" + HttpUtility.HtmlEncode(kv.Value) + "&";
                }
                oldori += "_aop_signature=" + signwithuri;
                return oldori;
            }
 
 
            //return "";
 
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值