欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# U8调用OpenAPI 20180810

程序员文章站 2022-07-05 11:46:56
...

U8 OpenApi调用

调用OpenAPI的准备:
一.在用友开放平台(http://open.yonyouup.com/)注册账号。
C# U8调用OpenAPI 20180810
二.申请成为开发者。
注册的时候选择开发者!
三.下载OpenAPI客户端。
http://open.yonyouup.com/下载客户端
四.创建应用。
选择你需要调用的接口
C# U8调用OpenAPI 20180810
五.申请测试账号。
申请的测试账号有效期为两个月
C# U8调用OpenAPI 20180810
六.U8开放平台配置应用需要调用的接口。
用测试账号登录
C# U8调用OpenAPI 20180810
七.配置OpenAPI客户端。
C# U8调用OpenAPI 20180810

完成准备后就可以开始项目的搭建了。

入库按钮
///
/// 入库按钮
///
///

    private void simpleButton1_Click(object sender, EventArgs e)
    {
        //string apiID = "0121503694";
        string url = "https://api.yonyouup.com/api/productin/add?";
        //fromAccount = this.fromaccountT.Text;
        //toAccount = this.toaccountT.Text;
        url += "from_account=" + fromAccount + "&to_account=" + toAccount + "&app_key=" + appKey
            + "&token=" + this.memoEdit1.Text.Trim() + "&tradeid=" + this.memoEdit2.Text.Trim();
        string postStr = @"{
                            ""productin"":{
                                ""warehousecode"":""01"",
                                ""date"":""2018-08-07"",
                                ""receivecode"":""2"",
                                ""departmentcode"":""1"",
                                ""maker"":""demo"",
                                ""entry"":[
                                    {
                                        ""inventorycode"":""2"",
                                        ""quantity"":""2"",
                                    }
                                ]
                            }
                        }";
        string returnMes = HttpPost(url, postStr);
        //LogFile.WriteLog("物理内存占用:" + Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0);
        JObject jo = JObject.Parse(returnMes);
        if (jo.Property("errcode") == null)
        {
            string a = jo["url"].ToString();
            getPostResult(jo["url"].ToString());
            MessageBox.Show("入库成功");
        }
        else if (jo.Property("errcode").Value.ToString() != "0")
        {
            MessageBox.Show(jo["errmsg"].ToString());
            //this.depT.Text = returnMes;
            return;
        }
    }

Post请求
///
/// Post请求
///

    CookieContainer cookie = new CookieContainer();
    private string HttpPost(string Url, string postDataStr)
    {
        ServicePointManager.ServerCertificateValidationCallback = new          System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
        byte[] data = Encoding.UTF8.GetBytes(postDataStr);
        Uri uRI = new Uri(Url);
        HttpWebRequest req = WebRequest.Create(uRI) as HttpWebRequest;
        req.Method = "POST";
        req.KeepAlive = true;
        req.ContentType = "application/json";
        req.ContentLength = data.Length;
        req.AllowAutoRedirect = true;
        Stream outStream = req.GetRequestStream();
        outStream.Write(data, 0, data.Length);
        outStream.Close();
        HttpWebResponse res = req.GetResponse() as HttpWebResponse;
        Stream inStream = res.GetResponseStream();
        StreamReader sr = new StreamReader(inStream, Encoding.UTF8);
        string htmlResult = sr.ReadToEnd();
        return htmlResult;
    }
    /// <summary>
    /// 取最好的post结果
    /// </summary>
    /// <param name="url"></param>

    private string getPostResult(string url)
    {
        string returnMes = HttpGet(url);
        //this.depT.Text = returnMes;
        JObject jo = JObject.Parse(returnMes);
        //LogFile.WriteLog("物理内存占用:" + Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0);
        if (jo.Property("errcode") == null)
        {
            getPostResult(jo["url"].ToString());
        }
        else if (jo.Property("errcode").Value.ToString() != "0")
        {
            MessageBox.Show(jo["errmsg"].ToString());
        }
        return returnMes;
    }

Get请求
///
/// Get请求
///
///
///

    public string HttpGet(string Url)
    {
        ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
        request.Method = "GET";
        request.ContentType = "text/html;charset=UTF-8";
        ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
        string retString = myStreamReader.ReadToEnd();
        myStreamReader.Close();
        myResponseStream.Close();
        return retString;
    }

    /// 验证服务器证书回调自动验证  
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="certificate"></param>
    /// <param name="chain"></param>
    /// <param name="errors"></param>
    /// <returns></returns>
    public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {   // 总是接受    
        return true;
    }

获取token
///
/// 获取token
///

    public void GetToken()
    {
        string url = "https://api.yonyouup.com/system/token?";
        fromAccount = this.fromaccountT.Text;
        appKey = this.appkeyT.Text;
        appSecret = this.appsecretT.Text;
        url += "from_account=" + fromAccount + "&app_key=" + appKey + "&app_secret=" + appSecret;
        returnToken = HttpGet(url);
        this.tokenT.Text = returnToken;
        JObject jo = JObject.Parse(returnToken);
        if (jo.Property("token") != null)
        {
            returnToken = jo["token"]["id"].ToString();
            this.memoEdit1.Text = returnToken;
        }
        else
        {
            MessageBox.Show(jo["errmsg"].ToString());
        }
    }
    #endregion

获取tradeId
///
///获取tradeid
///

    public void GetTradeId() 
    {
        string url = "https://api.yonyouup.com/system/tradeid?";
        fromAccount = this.fromaccountT.Text;
        appKey = this.appkeyT.Text;
        appSecret = this.appsecretT.Text;
        url += "from_account=" + fromAccount + "&app_key=" + appKey + "&token=" + returnToken;
        string returnTradeID = HttpGet(url);
        this.tokenT.Text = returnTradeID;
        JObject jo = JObject.Parse(returnTradeID);
        if (jo.Property("trade") != null)
        {
            tradeID = jo["trade"]["id"].ToString();
            this.memoEdit2.Text = tradeID;
        }
        else
        {
            MessageBox.Show(jo["errmsg"].ToString());
        }
    }
相关标签: U8调用OpenAPI