我正在向Content-Type
设置为的服务器发出POST请求application/x-www-form-urlencoded
。查询参数之一具有包含&
在其中的值。我更换&
与&
发送请求之前。
当我使用POSTMAN(Chrome扩展程序)发送此请求时,该请求正常进行,并且收到了预期的响应。但是,当我使用Java应用程序发送此请求时,服务器抛出错误(无法解析文档)。这是我用来发送请求的代码:
URL url = new URL(url); // url => "http://myserver.com/api/update"
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(60000);
urlConnection.setReadTimeout(60000);
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
PrintWriter printWriter = new PrintWriter(urlConnection.getOutputStream());
printWriter.print(params); // params => api_key=abcd123&update_data_id=123&update_data_value=Test&Value
printWriter.flush();
InputStream inputStream = urlConnection.getInputStream();
String contentType = urlConnection.getContentType();
FileUtil.closeWriter(printWriter);
// Parse response ...
在这里,问题出现在参数中update_data_value
。如果我删除了&
,则POSTMAN和我的应用程序的请求都很好。但是,当&
存在时,仅通过POSTMAN的请求可以正常工作。
有什么问题吗?
非常感谢你的帮助!
经过长时间的聊天,问题是这样的:
它是关于XML字符串的,其中使用了&符号。根据XML标准,“&”号需要替换为“&”。
此XML字符串需要在POST请求中发送,因此需要对与号和分号进行转义。
最终的替换字符串如下所示:“%26amp%3B”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句