请求

本文介绍了一种使用Java发送HTTP Post请求的方法,通过RestTemplate和HttpClient实现。演示了如何设置请求头,构建请求实体,以及处理响应。适用于后端开发人员学习HTTP请求的处理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
		map.add("mobileTel", mobileTel);
		map.add("custName", custName);
		logger.info("访问域名domain="+domain);
		//domain ="https://conttest.huijuer.com";
		String url = domain+"/api/huijuBusinessSearch/checkCustNameAndTelAndGetContract";
		logger.info("访问地址="+url);
		RestTemplate client = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		// 以表单的方式提交
		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
		ResponseEntity<Map> response = client.postForEntity( url, request , Map.class );
  public static String httpPostWithJson(Map<String, String> map, String url, String signer) {
        String responseContent = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        try {
            // 构造消息头Header
            httpPost.setHeader("Connection", "Close");
            httpPost.setHeader("Authorization", "Signer " + signer);
            // 构建消息实体
            //表单格式
            List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
                pairList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
            StringEntity entity = new UrlEncodedFormEntity(pairList, "utf-8");
            entity.setContentEncoding("UTF-8");
            httpPost.setEntity(entity);
            System.out.println("POST_URL:" + httpPost);
            HttpResponse response = httpClient.execute(httpPost);

            // 检验返回码
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity he = response.getEntity();
                responseContent = EntityUtils.toString(he, "UTF-8");
            } else {
                logger.info("调用接口url=" + url + ",返回码statusCode=" + statusCode);
            }
        } catch (Exception e) {
            logger.error("调用接口url=" + url + ",错误: " + e.getMessage());
        } finally {
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    logger.error("httpClient.close", e);
                }
            }
            if (httpPost != null) {
                try {
                    httpPost.releaseConnection();
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    logger.error("httpClient.close", e);
                    Thread.currentThread().interrupt();
                }
            }
        }
        return responseContent;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值