【发布时间】:2021-02-20 15:38:25
【问题描述】:
我有一个向接口发送 Web 服务请求的 java 代码。现在对于其中一个界面,我收到以下错误
java.lang.IllegalArgumentException: Illegal character(s) in message header field: http://10.11.22.33:8088/platform-core/smsGateMgw
at sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader(HttpURLConnection.java:522)
at sun.net.www.protocol.http.HttpURLConnection.isExternalMessageHeaderAllowed(HttpURLConnection.java:492)
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3057)
at com.in.ci.fioutbound.custom.SmsSender.sendSms(SmsSender1.java:255)
at com.in.ci.fioutbound.custom.SmsSender.executeOutboundRequest(SmsSender1.java:104)
我猜这是因为 URL 中的“-”,但不确定。 有人可以让我知道如何对 URL 的路径进行编码,或者是否有其他方法来处理这个问题。 这就是我的 java 代码现在的样子
URL smsServiceUrl = new URL(smsService);
URLConnection conn = smsServiceUrl.openConnection();
HttpURLConnection httpconn = (HttpURLConnection)conn;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[reqXml.length()];
buffer = reqXml.getBytes();
bout.write(buffer);
byte[] b = bout.toByteArray();
httpconn.setRequestProperty("Content-Length", String.valueOf(b.length));
httpconn.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
httpconn.setRequestProperty(smsService, smsService);
httpconn.setRequestMethod("POST");
httpconn.setDoOutput(true);
httpconn.setDoOutput(true);
OutputStream out = httpconn.getOutputStream();
out.write(b);
InputStreamReader isr = new InputStreamReader(httpconn.getInputStream());
BufferedReader in = new BufferedReader(isr);
while ((responseString = in.readLine()) != null) {
outputString = outputString + responseString;
}
此外,相同的代码在具有相同 URL 的另一台服务器上也能正常工作。唯一的区别是两个服务器之间的 java 1.7 和 1.8。那么这是 java 1.8 中的新功能吗?
【问题讨论】:
标签: java url httpconnection