java如何调用对方http接口(header和body中的参数传参)

public class IdentitycardTwo {
/**
* 发送post请求
* @param url
* 请求地址
* @param body
* 参数 以json串形式 ,参数详情见 接口文档
* @return
* @throws IOException
* @throws ClientProtocolException
* @throws Exception
*/
public String sendHttpPost(Idcards idcard) throws Exception{
// 从properties中读取数据

    	String serverURL = SealInfoUtil.getSealInfoDetail().getProperty("bairong.url");//从配置文件中读取路径  可以写死     "http://。。。。。。。"
		StringBuffer sbf = new StringBuffer(); 
		String strRead = null; 
		URL url = new URL(serverURL); 
		HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
		connection.setRequestMethod("POST");//请求post方式
		connection.setDoInput(true); 
		connection.setDoOutput(true); 
		//header内的的参数在这里set    connection.setRequestProperty("健, "值");
		connection.setRequestProperty("Content-Type", "application/json");
		connection.setRequestProperty("Info_Token", idcard.getInfoToken());
		
		connection.connect(); 
		OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8"); 

		//body参数在这里put到JSONObject中
		
		JSONObject parm = new JSONObject();
    	parm.put("name", idcard.getName());
    	parm.put("idNo", idcard.getIdNo());
		writer.write(parm.toString()); 
		writer.flush();
		InputStream is = connection.getInputStream(); 
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); 
		while ((strRead = reader.readLine()) != null) { 
			sbf.append(strRead); 
			sbf.append("\r\n"); 
		}
		reader.close(); 
		connection.disconnect();
		String results = sbf.toString();
		return results;
	}

}

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
引用\[1\]描述了使用FeignClient进行远程调用时,如果POST请求有查询参数并且没有请求实体(body为空),那么查询参数会被丢失,服务提供者获取不到查询参数的值。在代码描述,可以看到在FeignClient接口的方法使用了@RequestParam注解来传递查询参数。然而,由于FeignClient默认使用的是POST请求,而POST请求的查询参数是通过请求体(body)传递的,所以查询参数会被丢失。 要同时在headerbody传递参数,可以使用@RequestHeader注解来传递header参数,使用@RequestBody注解来传递body参数。例如,可以将B作为请求体的一部分,同时将其他参数作为请求头的一部分进行传递。 下面是一个示例代码: ```java @FeignClient(name = "a-service", configuration = FeignConfiguration.class) public interface ACall { @RequestMapping(method = RequestMethod.POST, value = "/api/xxx/{A}", headers = {"Content-Type=application/json"}) void updateAToB(@PathVariable("A") final String A, @RequestHeader("B") final String B, @RequestBody final Object body) throws Exception; } ``` 在这个示例,使用@RequestHeader注解将B作为请求头的一部分传递,使用@RequestBody注解将其他参数作为请求体的一部分传递。 这样,就可以同时在headerbody传递参数了。 #### 引用[.reference_title] - *1* [FeignClient调用POST请求时查询参数被丢失的情况分析与处理](https://blog.csdn.net/hui008/article/details/82842720)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Java——使用@FeignClient调用远程服务](https://blog.csdn.net/wang__sepcial/article/details/122171795)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [@FeignClient 调用远程服务](https://blog.csdn.net/m0_38017766/article/details/94481713)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值