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

        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;
        }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是使用C#HttpClient发送POST请求的示例代码: ```csharp using System; using System.Net.Http; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { // 创建HttpClient实例 using (HttpClient client = new HttpClient()) { // 设置请求的基本信息,例如URL和内容类型 string url = "http://example.com/api/endpoint"; string contentType = "application/json"; // 构建要发送的数据 string requestBody = "{\"key1\": \"value1\", \"key2\": \"value2\"}"; // 设置请求头部,包括内容类型和其他自定义头部(如果需要) client.DefaultRequestHeaders.Add("Content-Type", contentType); // client.DefaultRequestHeaders.Add("Authorization", "Bearer token"); // 发送POST请求并获取响应 HttpResponseMessage response = await client.PostAsync(url, new StringContent(requestBody)); // 确保请求成功 response.EnsureSuccessStatusCode(); // 读取响应内容 string responseBody = await response.Content.ReadAsStringAsync(); // 处理响应数据 Console.WriteLine(responseBody); } } } ``` 在上述代码中,我们首先创建了一个HttpClient实例。然后,我们设置了请求的基本信息,如URL和内容类型。接下来,我们构建要发送的数据,并设置请求头部,包括内容类型和其他自定义头部(如果需要)。最后,我们发送POST请求并获取响应。确保请求成功后,我们读取响应内容,并对其进行处理。 请注意,在实际使用中,您可能需要根据API的要求进行适当的修改,例如更改URL、内容类型、请求头部等。此示例仅为了说明如何使用C#HttpClient发送POST请求

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值