我使用以下代码将数据发送到servlet: 当encoding =“UTF-8”或“GBK”时,数据被正确接收。 但是当encoding =“UTF-16”时,接收器接收null。 WHY ??
发件人:
URL url = new URL(notifyURL);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=" + encoding);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("notify_id=" + URLEncoder.encode("123", encoding) + "¬ify_type=" + URLEncoder.encode("any", encoding));
out.flush();
out.close();
connection.connect();
接收器servlet:
log.info(request.getParameter("notify_type")); //print null
答案 0 :(得分:1)
你有2个问题,
答案 1 :(得分:1)
根据URLEncoder的Javadocs,您应该只使用UTF-8,因为其他编码可能会导致问题。它们直接链接到Javadocs的W3C规范。