post json 做参数

post json 做参数

@RequestMapping(value = "/record")
    @ResponseBody
    public String record(@RequestParam(value = "token")String token ,
                         @RequestParam(value = "elevatorId")String elevatorId,
                         String startTimeStr ,String endTimeStr) throws Exception {

        CommonResult commonResult = new CommonResult();
        String username=huaxiaContext.getHuaxiaUsername();
        String password = huaxiaContext.getHuaxiaPassword();
        String url = huaxiaContext.getHuaxiaUrl();
        try {

            Elevator_Elevator elevator = (Elevator_Elevator) RedisUtil.getTableData(usercenterContext.getRedisServerAddr(), usercenterContext.getRedisServerPort(),usercenterContext.getRedisServerPass(), Elevator_Elevator.class,elevatorId);
            String sbId = elevator.getDb_videoMonitorDeviceId();
            commonResult = JSON.parseObject( login(),CommonResult.class);
            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.setContentType(MediaType.APPLICATION_JSON);
            requestHeaders.put(HttpHeaders.AUTHORIZATION, Collections.singletonList("Bearer " + commonResult.getData().toString()));


            JSONObject requestJson = new JSONObject();
            requestJson.put("deviceId", sbId);
            requestJson.put("beginTime", startTimeStr);
            requestJson.put("endTime", endTimeStr);



            HttpEntity<JSONObject> requestBody = new HttpEntity<>(requestJson, requestHeaders);
            RestTemplate restTemplate = new RestTemplate(generateHttpRequestFactory());
            Object data= restTemplate.postForObject(url, requestBody, Object.class);
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(data));

            if("200".equals(jsonObject.get("retCode").toString())){
                JSONObject jsonObject2 = JSONObject.parseObject( jsonObject.get("retCode").toString());
                commonResult.setData(jsonObject2);
            }else{
                commonResult.setSuccess(false);
                commonResult.setData(jsonObject.toString());
            }

        }catch (Exception e){
            logger.error(e.getMessage());
            commonResult.setSuccess(false);
            commonResult.setData("设备已离线");

        }
        return objectMapper.writeValueAsString(commonResult);
    }





    /**
     * 通过该工厂类创建的RestTemplate发送请求时,可忽略https证书认证
     * @return 工厂
     */
    private static HttpComponentsClientHttpRequestFactory generateHttpRequestFactory() throws Exception {
        TrustStrategy acceptingTrustStrategy = ((x509Certificates, authType) -> true);
        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
        SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());

        HttpClientBuilder httpClientBuilder = HttpClients.custom();
        httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
        CloseableHttpClient httpClient = httpClientBuilder.build();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(httpClient);
        return factory;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在WinForm中进行HTTP POST请求并传递JSON参数可以通过使用System.Net.Http类库来实现。 首先,需要在项目中添加对System.Net.Http和System.Net.Http.Headers的引用。 然后,使用HttpClient类来发送HTTP请求。以下是一个示例代码: ```csharp using System; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; ... public async Task<string> PostJson(string url, string jsonData) { using (var httpClient = new HttpClient()) { // 设置请求头为application/json httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // 构建请求内容 var content = new StringContent(jsonData); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // 发送请求 var response = await httpClient.PostAsync(url, content); // 读取响应内容 var responseContent = await response.Content.ReadAsStringAsync(); return responseContent; } } ``` 在调用PostJson方法时,需要传递目标URL和要发送的JSON数据作为参数。该方法将返回一个包含服务器响应的字符串。 使用示例: ```csharp string url = "http://example.com/api/data"; string jsonData = "{\"name\":\"John\",\"age\":30}"; string response = await PostJson(url, jsonData); // 处理响应 Console.WriteLine(response); ``` 以上代码将发送一个POST请求到指定的URL,并将JSON数据作为参数发送。然后,它将等待服务器的响应,并将响应内容存储在response变量中,你可以根据需要进行进一步的处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值