POST请求传入中文参数,接收端乱码

问题描述:通过post请求调试短信接口发送出去后,客户端无法收到短信,中文内容乱码

追踪过程:

  1. 接口采用post请求进行,无法收取短信的接口代码如下:
  2. public static String sendPost(String url, Map<String, String> paramMap) {PrintWriter out = null;BufferedReader in = null;String result = "";try {URL realUrl = new URL(url);// 打开和URL之间的连接URLConnection conn = realUrl.openConnection();// 设置通用的请求属性conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");conn.setRequestProperty("Charset", "UTF-8");// 发送POST请求必须设置如下两行conn.setDoOutput(true);conn.setDoInput(true);// 获取URLConnection对象对应的输出流out = new PrintWriter(conn.getOutputStream());// 设置请求属性StringBuilder param = new StringBuilder();if (paramMap != null && paramMap.size() > 0) {Iterator<String> ite = paramMap.keySet().iterator();while (ite.hasNext()) {String key = ite.next();// keyString value = paramMap.get(key);param.append(key).append("=").append(value).append("&");}param = new StringBuilder(param.substring(0, param.length() - 1));}// 发送请求参数out.print(param);// flush输出流的缓冲out.flush();// 定义BufferedReader输入流来读取URL的响应in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {System.err.println("发送 POST 请求出现异常!" + e);e.printStackTrace();}// 使用finally块来关闭输出流、输入流finally {try {if (out != null) {out.close();}if (in != null) {in.close();}} catch (IOException ex) {ex.printStackTrace();}}return result;}

    其中的参数paramMap如下:
     

           Map<String, String> params = new HashMap<String, String>();params.put("userid", userid);params.put("ts", ts);params.put("sign", md5.toLowerCase());params.put("mobile", phones);params.put("msgcontent", "【您好!请注意】" + content);params.put("extnum", sendtermid);params.put("time", sendtime);

    问题的源头是其中的key 为msgContent的value中含有中文,而中文内容如果不通过URLEncoder.encode(param,"UTF-8"),"UTF-8")进行编码设置,那么会被默认的通过其他编码格式进行编码后发送出去,导致短信平台服务器接收到的短信内容是乱码

  3. 解决方案:在入参时进行UTF-8编码处理
URLEncoder.encode(URLEncoder.encode(param,"UTF-8"),"UTF-8"))

服务端获取参数时进行一次解码

 String param= URLDecoder.decode(param, "UTF-8");

感谢文章:https://my.oschina.net/gschen/blog/120553 

https://www.zhongjianghua.com/att1tude/131194-2020-12.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/419196.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode - Container With Most Water

题目&#xff1a; Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms…

TCP/IP协议模型

1. 数据链路层 作用(1) 实现网卡接口的网络驱动&#xff0c;以处理数据在以太网线等物理媒介上的传输   (2) 网络驱动程序隐藏了不同物理网络的不同电气特性&#xff0c;为上层协议提供一个统一的接口 应用ARP和RARP(Reverse Address Resolve Protocol)即逆地址解析协议&am…

【转】 Pro Android学习笔记(二九):用户界面和控制(17):include和merge

目录(?)[-] xml控件代码重用includexml控件代码重用merge横屏和竖屏landsacpe portraitxml控件代码重用&#xff1a;include 如果我们定义一个控件&#xff0c;需要在不同的layout中重复使用&#xff0c;或者在同一个layout中重复使用&#xff0c;可以采用include的方式。例如…

管理者一定会遇到的那些事

——极客时间——沈剑老师分享有感

Git使用攻略

Git使用攻略 merge&#xff0c;将develop合并到master# 切换到Master分支git checkout master# 将Develop分支合并到master git merge --no-ff develop 切换分支git checkout master 检出代码git checkout master 创建分支git branch newBranch查看本地分支git branch查看远程分…

Java停止线程的方式

1、使用中断标志位 public class StopThreadTest extends Thread {private boolean exit false;Overridepublic void run() {while (!exit) {try {System.out.println("i am running,please wait a moment");Thread.sleep(500);} catch (InterruptedException e) {…

Vim文本编辑器 指令簿(二)

常常处理文本以及常常须要写代码的人&#xff0c;都会有自己比較常常使用的编辑器&#xff0c;本人喜欢用Vim。理由就是Vim编辑器灵活&#xff0c;而且能够达到纯键盘操作&#xff0c;使用纯熟情况下&#xff0c;根本不须要鼠标操作。听起来是不是非常酷的&#xff1f;只是别高…

读《数学之美》

数学之美 数学的发展实际上是不断的抽象和概括的过程 目录 数学之美 第一章 第二章&#xff08;从规则到统计&#xff09; 第三章 统计语言模型 第四章 分词 第五章 隐马尔科夫模型 第六章 信息的度量和作用 第七章 贾里尼克和和现代语言处理 第八章 简单之美——布…

每天一个JavaScript实例-动态省份选择城市

<!DOCTYPE html> <html> <head> <meta http-equiv"Content-Type" content"text/html; charsetUTF-8" /> <title>每天一个JavaScript实例-动态省份选择城市</title> <script> var citystore new Array(); citys…

阅读源码那些事

方法 看spring的源码的时候如果我们一直追究所有的细节那会让我们会越陷越深&#xff0c;掉入细节的无底洞&#xff0c;稍不留神脑回路跟不上就会蒙圈。 因此&#xff0c;我们要学会找源码中的关键部分看&#xff0c;弄懂主要流程和本次看源码的目的的那部分就行。 等我们对…

Advanced C++ --- const function

上一篇介绍了const修饰的变量或者指针的含义&#xff0c;这篇我们介绍const修饰的函数以及函数参数含义。 首先我们看一个例子 class Dog{int age;string name; public:Dog(){age 3;name "dummy";}void setAge(const int &a){age a;a;} };int main(){Dog d;in…

slf4j、log4j日志报错排查

1、WARN Please initialize the log4j system properly 解法&#xff1a;只要在 src文件目录下建立配置文件log4j.properties即可 2、SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" 解法&#xff1a;这是因为sl4j和log4j的不兼容导致的&#…

无刷新三级联动查询

JQuery中国省市区无刷新三级联动查询&#xff08;转&#xff09;&#xff1a;http://www.cnblogs.com/xiaoyu5062/archive/2012/07/30/2615359.html Ajax实现无刷新三联动下拉框&#xff08;转&#xff09;&#xff1a;http://singlepine.cnblogs.com/articles/257954.html js特…