C# 调用服务器接口

请求的接口文档

Url: https://nesting.lyntime.com/optimized_nesting/nesting
Method: POST
Header:
Content-Type:application/json
AppKey:填入申请的AppKey
Authorization:Bearer token (注意Bearer后有一个空格)

Json Body 示例: 下面就是要传递的参数Body
{
“parts”: [
{
“dxf_path”: “dxf/1-y30.dxf”,
“part_name”: “1-y30.dxf”,
},
{
“part_name”: “2-y3.dxf”,
“dxf_path”: “dxf/2-y3.dxf”
}
],
“advance_direction_max_part_depth”: 450,
“sheet_length”: 2440,
“sheet_width”: 1220
}

代码

//调用接口(PostURl:接口地址,Body:JSON数据,key:钥匙,token:请求所需的token)
 public string PostWebReq(string PostUrl, string Body, string Key, string token)
        {           
            try
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(Body);  //将字符串编为一个序列
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;  //安全包支持的安全协议
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(PostUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/json";
                webReq.ContentLength = byteArray.Length;
                webReq.Headers.Add("AppKey", Key); //在Headers 头部添加AppKey 参数
                if (!string.IsNullOrEmpty(token))
                {
                    token = string.Concat("Bearer ", token);  //组合字符串
                    webReq.Headers.Add("Authorization", token); //在Headers 头部添加Authorization 参数
                }
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                string ReturnVal = reader.ReadToEnd();
                reader.Close();
                response.Close();
                webReq = null;
                return ReturnVal;
            }
            catch (Exception ex)
            {
                throw new Exception("接口:"+ex.Message);
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南墙已撞

你的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值