C# 窗体通过调用webapi往U9C系统创建请购单

发布于:2024-08-17 ⋅ 阅读:(106) ⋅ 点赞:(0)

C# 窗体通过调用webapi往U9C系统创建请购单

窗体图片
界面

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace u9capitest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public string sqtoken;
        private async void button1_Click(object sender, EventArgs e)
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Get,
                RequestUri = new Uri("http://127.0.0.1/u9c/webapi/OAuth2/GetAuthorizeCode?clientid=AP&clientsecret=5cf3d45619a6440699b8aa341d86a146"),//需要用EA账号建立API和Key
            };
            using (var response = await client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();
                var body = await response.Content.ReadAsStringAsync();
                Getsqm getsqm = JsonConvert.DeserializeObject<Getsqm>(body);
                //MessageBox.Show(getsqm.Data);
                if (getsqm.Data!="")
                {
                    var client1 = new HttpClient();
                    var request1 = new HttpRequestMessage
                    {
                        Method = HttpMethod.Get,
                        RequestUri = new Uri(string.Format("http://127.0.0.1/u9c/webapi/OAuth2/Login?userCode=admin&entcode=01&orgcode=101&code={0}", getsqm.Data)),//userCode替换自己的用户,entcode许可软件上的企业代码,如果只有一个默认是01,orgcode是组织代码自己查

                    };
                    using (var response1 = await client.SendAsync(request1))
                    {
                        response1.EnsureSuccessStatusCode();
                        var body1 = await response1.Content.ReadAsStringAsync();
                        Getsqm getsqm1 = JsonConvert.DeserializeObject<Getsqm>(body1);
                        if (getsqm1.Data!="")
                        {
                            sqtoken = getsqm1.Data;
                        }


                    }

                }
            }
            if (sqtoken!="")
            {
                var client3 = new HttpClient();
                var request3 = new HttpRequestMessage
                {
                    Method = HttpMethod.Post,
                    RequestUri = new Uri("http://127.0.0.1/u9c/webapi/PR/Create"),
                    Headers =
    {
        { "token", sqtoken },
    },
                    Content = new StringContent("[\r\n  {\r\n    \"PRDocType\": \"PR1\",\r\n    \"DocNo\": \"\",\r\n    \"BusinessDate\": \"2024-08-14\",\r\n    \"PRLineDatas\": [\r\n      {\r\n        \"DocLineNo\": \"10\",\r\n        \"ItemCode\": \"31112-00015\",\r\n        \"ReqDept\":\"690000\",\r\n        \"ReqEmployee\":\"20240128\",\r\n        \"TradeUOM\": \"个\",\r\n        \"PriceUOM\": \"个\",\r\n        \"ReqQtyPU\": 10,\r\n        \"ApprovedQtyPU\": 10,\r\n        \"ReqQtyTU\": 10,\r\n        \"ApprovedQtyTU\": 10,\r\n        \"RequiredDeliveryDate\": \"2024-08-14\",\r\n        \"RegOrg\": \"001\"\r\n      }\r\n    ],\r\n    \"OtherID\": \"\"\r\n  }\r\n]")//查看官方得api参数
                    {
                        Headers =
        {
            ContentType = new MediaTypeHeaderValue("application/json")
        }
                    }
                };
                using (var response3 = await client.SendAsync(request3))
                {
                    response3.EnsureSuccessStatusCode();
                    var body2 = await response3.Content.ReadAsStringAsync();
                    //Getsqm getsqm3 = JsonConvert.DeserializeObject<Getsqm>(body2);
                    JObject pasedJson = new JObject();
                    pasedJson = JObject.Parse(body2);
                    JArray dataArray = (JArray)pasedJson["Data"];
                    CreatDate creatDate = new CreatDate();
                    foreach (JObject idatem in dataArray)
                    {
                        if (idatem["IsSucess"].ToString()!="")
                        {
                            creatDate.IsSucess = idatem["IsSucess"].ToString();
                        }
                        if (idatem["Success"].ToString() != "")
                        {
                            creatDate.Success = idatem["Success"].ToString();
                        }
                        if (idatem["OtherID"].ToString() != "")
                        {
                            creatDate.OtherID = idatem["OtherID"].ToString();
                        }
                        if (idatem["ID"].ToString() != "")
                        {
                            creatDate.ID = idatem["ID"].ToString();
                        }
                        if (idatem["Code"].ToString() != "")
                        {
                            creatDate.Code = idatem["Code"].ToString();
                        }
                        if (idatem["ErrorMsg"].ToString() != "")
                        {
                            creatDate.ErrorMsg = idatem["ErrorMsg"].ToString();
                        }

                    }
                    //CreatDate creatDate = JsonConvert<CreatDate>(dataArray);
                    MessageBox.Show(creatDate.Code);
                }
            }


        }
    }
}

工程文件源码
https://download.csdn.net/download/weixin_43050480/89645082


网站公告

今日签到

点亮在社区的每一天
去签到