JAVA HTTP中POST请求带参数

JAVAHTTPPOST请求:参数传递与ResponseEntity处理

JAVA HTTP中POST请求带参数
通过设置url地址
HashMap为参数
ResponseEntity执行

 //请求路径
            String url = "http://" + ipAddress + "/action/SearchPersonList";
            //String url = "http://127.0.0.1:4523/m1/2699806-0-default/action/SearchPersonList";
            //参数
            Map<String, Object> mapOpen = new HashMap<>();
            mapOpen.put("PersonType", 0);
            mapOpen.put("Picture", 1);
            Map<String, Object> params = new HashMap<>();
            params.put("operator", "SearchPersonList");
            params.put("info", mapOpen);
            //使用 RestTemplate 发送 HTTP 请求
            RestTemplate client = new RestTemplate();
            //使用 POST 方式发送请求
            HttpMethod method = HttpMethod.POST;
            // 配置头信息
            HttpHeaders headers = new HttpHeaders();
            // 以 raw json 方式提交
            headers.setContentType(MediaType.APPLICATION_JSON);
            // 认证
            headers.setBasicAuth("admin", "mcky9999");
            //将请求头部和参数合成一个请求
            HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(params, headers);
            //执行 HTTP 请求
            ResponseEntity<String> responseEntity = client.exchange(url, method, httpEntity, String.class);
            //TODO:以下代码待用到时做返回值判断,当前认为没什么必要做判断
            //返回状态码
            Integer intA = responseEntity.getStatusCodeValue();
            //返回Body内容
            String strA = responseEntity.getBody();
            String strA = responseEntity.getBody();
			JSONObject jsonObject = JSONObject.parseObject(strA);
			String versionInfoStr = jsonObject.getString("info");
			JSONObject infoObject = JSONObject.parseObject(versionInfoStr);
			String infoList = infoObject.getString("List");
			List<InFoNumber> inventoryDTOs = JSON.parseArray(infoList, InFoNumber.class);
			for (int i = 0;i < inventoryDTOs.size();i++){
			   generateImage(inventoryDTOs.get(i).getPicinfo(),inventoryDTOs.get(i).getMjCardNo());
			}
Java中发送带有参数的POST请求,可以通过`HttpURLConnection`类实现。该类提供了对HTTP协议的支持,可以用于发送GET、POST等类型的请求。发送POST请求时,通常需要设置请求头中的`Content-Type`字段以匹配请求体的数据格式,并启用`setDoOutput(true)`来表明请求需要输出数据[^3]。 以下是一个发送包含参数的POST请求的示例,请求体中的数据格式为`application/x-www-form-urlencoded`: ```java import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class PostRequestExample { public static void main(String[] args) throws Exception { URL url = new URL("http://example.com/api"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setDoOutput(true); String urlParameters = "param1=value1&param2=value2"; try (OutputStream os = connection.getOutputStream()) { byte[] input = urlParameters.getBytes("utf-8"); os.write(input, 0, input.length); } int responseCode = connection.getResponseCode(); System.out.println("Response Code: " + responseCode); } } ``` 如果请求体的数据格式为JSON,可以通过调整`Content-Type`字段为`application/json`,并构造相应的JSON字符串来实现: ```java import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class JsonPostRequestExample { public static void main(String[] args) throws Exception { URL url = new URL("http://example.com/api"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json; utf-8"); connection.setDoOutput(true); String jsonInputString = "{\"param1\":\"value1\",\"param2\":\"value2\"}"; try (OutputStream os = connection.getOutputStream()) { byte[] input = jsonInputString.getBytes("utf-8"); os.write(input, 0, input.length); } int responseCode = connection.getResponseCode(); System.out.println("Response Code: " + responseCode); } } ``` 在实际应用中,可能还需要处理服务器返回的具体响应内容,这可以通过读取`InputStream`或`ErrorStream`来完成,具体取决于请求的成功与否。此外,应确保在网络请求过程中妥善处理可能出现的异常情况,例如网络中断、超时等[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值