以json格式发回斜杠字符,响应码500
我正在尝试通过 http 上的 JSONObject 发送消息。
JSONObject s=new JSONObject();
s.put("addresses", sno);
s.put("message",message);
s.put("senderName",shortcode);
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write( s.toJSONString());
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
消息如下:
String message= “amount1\\KB \n amount2\\KB”
预期收到的消息:
amount1\KB
amount2\KB
现在问题是http服务器返回我响应代码500。并在第9行引起异常。 注意:问题区域是字符串部分 \KB,如果字符串不包含此反斜杠,则一切正常。
我尝试过:
message = message.replaceAll("\\\\", "\");
message = message.replaceAll("\\\\", "%5C");
它不会导致我的任何异常,但收到的消息如下:
amount1\KB
amount2\KB
并且
amount1%5CKB
amount2%5CKB
JSONObject.escape(message)
也不起作用。
I am trying to send a message via JSONObject over http.
JSONObject s=new JSONObject();
s.put("addresses", sno);
s.put("message",message);
s.put("senderName",shortcode);
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write( s.toJSONString());
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Message is like:
String message= “amount1\\KB \n amount2\\KB”
Expected received message:
amount1\KB
amount2\KB
Now problem is http server returns me reponse code 500. And cause exception at line 9.
Note: Problem area is String portion \KB if String would not be containing this back slash everything goes fine.
I had tried:
message = message.replaceAll("\\\\", "\");
message = message.replaceAll("\\\\", "%5C");
it do not caused my any exception but the received message was as following:
amount1\KB
amount2\KB
and
amount1%5CKB
amount2%5CKB
JSONObject.escape(message)
also didn’t work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,您已经写了为什么在显示您的回复之前将 %5C 替换为 \ ?
Well from that you've written why you just replace back %5C with \ before your show your response?