HttpURLConnection URL BufferedReader InputStream 发送请求 解决字符编码问题

@RequestMapping(path = "/v1/ht", produces = {"application/json;charset=UTF-8"})

@PostMapping("/htInsuredSubmit")
public ResultDTO<Map<String, String>> htInsuredSubmit(@RequestBody String strAppInfo,HttpServletRequest request){
try {
LogHelper.commoninfo("==========API==strAppInfo===="+strAppInfo);
// 针对url传递特殊字符转译问题
strAppInfo = strAppInfo.replaceAll("%","%25");
LogHelper.commoninfo("==========API==strAppInfo===="+strAppInfo);
String signature = MD5Util.MD5Encode(strAppInfo + ENCRYPT_VAL, "utf-8"); // 加密
// 发送数据,获取响应
    String jsonStr = HttpClientConfig.requestService(htInsuredSubmitUrl, "json="+strAppInfo+"&channelCode="+CHANNELCODE+"&signature="+signature);
return new ResultDTO<>(ResultDTO.CODE_SUCCESS,jsonStr);
} catch (Exception e) {
LogHelper.commonerror("HrInsuredController.htInsuredSubmit:" + e.getMessage(), e);
return new ResultDTO<>(ResultDTO.CODE_DEFAULT_FAIL, "投保失败,请重试!");
}


public static String requestService(String toURL,String data) throws Exception {
StringBuffer bs = new StringBuffer();
URL url = new URL(toURL);
HttpURLConnection urlcon=null;
String proxyUrl="10.9.1.5";
int proxyProt=3128;
// 打开和URL之间的连接
            if(isProxy){
                Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyUrl, proxyProt));
            urlcon = (HttpURLConnection) url.openConnection(proxy);
            }else{
            urlcon = (HttpURLConnection) url.openConnection();
            }  
urlcon.setRequestMethod("POST");
urlcon.setUseCaches(false);
urlcon.setConnectTimeout(30000);
urlcon.setReadTimeout(30000);
urlcon.setDoInput(true);
urlcon.setDoOutput(true);
urlcon.setRequestProperty("accept", "*/*");
urlcon.setRequestProperty("Accept-Charset", "UTF-8");
urlcon.setRequestProperty("contentType", "UTF-8");
urlcon.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
OutputStream out = urlcon.getOutputStream();
out.write(data.getBytes("UTF-8"));
out.flush();
out.close();
urlcon.connect();
InputStream is = urlcon.getInputStream();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(is,"UTF-8"));
String l = null;
while ((l = buffer.readLine()) != null) {
bs.append(l);
}
return bs.toString();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用HttpURLConnection发送多个参数到POST请求,可以按照以下步骤进行: 1. 创建一个URL对象,并指定POST请求URL地址。 2. 调用openConnection()方法创建HttpURLConnection对象,并设置请求方法为POST。 3. 设置HttpURLConnection对象的参数,如设置连接超时时间、读取超时时间等。 4. 设置HttpURLConnection对象的请求头,指定Content-Type为application/x-www-form-urlencoded。 5. 构建请求参数字符串,将多个参数按照key-value的形式用&符号连接起来。 6. 获取HttpURLConnection对象的输出流,将请求参数字符串写入输出流中。 7. 调用HttpURLConnection对象的getInputStream()方法获取响应数据的输入流。 8. 读取响应数据的输入流,将响应数据换为字符串并返回。 下面是一个示例代码(假设要发送两个参数name和age): ``` URL url = new URL("http://example.com/api"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); String params = "name=" + URLEncoder.encode("张三", "UTF-8") + "&age=" + URLEncoder.encode("20", "UTF-8"); OutputStream os = conn.getOutputStream(); os.write(params.getBytes("UTF-8")); os.flush(); InputStream is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); conn.disconnect(); String result = response.toString(); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值