C#Httpclient post请求发送requestBody json参数

文章描述了一个C#函数,通过HttpClient向特定URL发送包含多个字段的json数据,用于生产数据处理,涉及OAuth2.0令牌验证。
摘要由CSDN通过智能技术生成
        public static async Task<string> SendData3(string URL, string jsonParam)
        {
            string result = ""; 
            string URLToken = $"https://192.168.111.111:1111/BSAPI/V1/ProductionData/getToken?machineNO=sponge01&key=28853ACB-FA6A-47A5-9B22-9578880290BD";
            string Token = await testGetToken(URLToken, "");

            var handler = new HttpClientHandler();
            handler.ServerCertificateCustomValidationCallback = delegate { return true; };
            using (var client = new HttpClient(handler))
            {
                var request = new HttpRequestMessage(HttpMethod.Post, URL);
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Token);
                request.Content = new StringContent(jsonParam, Encoding.UTF8, "application/json");

                var response = await client.SendAsync(request);
                result = await response.Content.ReadAsStringAsync();

                //Console.WriteLine(responseContent);
            }
            return result;
        }

发送json参数数据 


        /// <summary>
        /// 发送数据
        /// </summary> 
        /// <returns></returns>
        [HttpPost(Name = "SendData")]
        public async Task<string> SendData()
        {
            string jsonparam = "{\"destination\": \"test\",\"sourceData\": \"stri\",\"dateYear\": \"2025\", \"dateMonth\": \"11\", \"dateDay\": \"25\", \"dateHour\": \"22\", \"dateMin\": \"22\", \"dateSec\": \"22\", \"machineNo\": \"st\", \"spare01\": \"st\", \"spare02\": \"stri\", \"objectiveMachineNo1\": \"string\", \"barcode\": \"strings\", \"processNo\": \"st\", \"tireSizeNotation\": \"string\", \"productType\": \"st\", \"tireOuterDiameter\": \"stri\", \"tireInnerDiameter\": \"stri\", \"tireWidth\": \"stri\", \"beadToBeadWidth\": \"stri\", \"spongeLength\": \"st\", \"spongeWidth\": \"st\", \"spongeHeight\": \"st\", \"connectorWidth\": \"st\", \"spare03\": \"stri\", \"spare04\": \"stri\", \"spare05\": \"stri\", \"spare06\": \"stri\", \"cameraLoadXPos\": \"stri\", \"cameraLoadYPos\": \"stri\", \"cameraLoadZPos\": \"stri\", \"cameraInputPos\": \"stri\", \"tireRotatingSpeed\": \"stri\", \"spare07\": \"stri\", \"spare08\": \"stri\", \"spare09\": \"stri\", \"upperLimitValueOfSpongeHeight\": \"st\", \"lowerLimitValueOfSpongeHeight\": \"st\", \"upperLimitOfSpongeGapWidth\": \"st\", \"lowerLimitOfSpongeGapWidth\": \"st\", \"upperLimitOfSpongeAttachmentCenterOffset\": \"st\", \"lowerLimitOfSpongeAttachmentCenterOffset\": \"st\", \"snakeShapedUpperLimit\": \"st\", \"upperLimitOfSegmentDifference\": \"st\", \"spare10\": \"st\", \"spare11\": \"st\", \"spare12\": \"stri\", \"objectiveMachineNo2\": \"string\", \"barcode2\": \"string\", \"processNo2\": \"st\", \"tireSizeNotation2\": \"string\", \"productType2\": \"st\", \"cameraLoadXPos2\": \"stri\", \"cameraLoadYPos2\": \"stri\", \"cameraLoadZPos2\": \"stri\", \"cameraInputPos2\": \"stri\", \"tireRotatingSpeed2\": \"stri\", \"wrinkleStripeDetectionResults\": \"st\", \"averageSpongeHeight\": \"st\", \"spongeHeightDeterminationResult\": \"st\", \"spongeGapWidth\": \"st\", \"spongeGapWidthDeterminationResult\": \"st\", \"maximumSpongeCenterOffset\": \"st\", \"spongeCenterOffsetDeterminationResult\": \"st\", \"spongeSnakeOffset\": \"st\", \"spongeSnakeShapedOffsetDeterminationResult\": \"st\", \"spongeSegmentDifference\": \"st\", \"spongeSegmentDifferenceResult\": \"st\", \"overallJudgmentResult\": \"0\", \"spare13\": \"st\", \"spare14\": \"st\", \"tireReception\": \"st\", \"tireFixationTireClawsRising\": \"st\", \"tireFixationTireClawsextended\": \"st\", \"cameraDescentZdirection1\": \"st\", \"cameraLateralMoveXdirection1\": \"st\", \"cameraLateralMoveYdirection1\": \"st\", \"tireRotationDetection\": \"st\", \"cameraLateralMoveYdirection2\": \"st\", \"cameraLateralMoveXdirection2\": \"st\", \"cameraDescentZdirection2\": \"st\", \"tireFixationTireClawsRising2\": \"st\", \"tireFixationTireClawsDown\": \"st\", \"tireOut\": \"st\", \"totalTime\": \"st\", \"spare15\": \"st\", \"spare16\": \"st\", \"spare17\": \"st\"}";
            string URL = $"https://192.168.65.48:6001/BSAPI/V1/ProductionData/SendData";
            string result = await HttpClientHelper.SendData3(URL, jsonparam);
        
            return result;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值