Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
URL中的java.lang.IllegalArgumentException';Java中的s路径_Java_Url_Httpconnection - Fatal编程技术网

URL中的java.lang.IllegalArgumentException';Java中的s路径

URL中的java.lang.IllegalArgumentException';Java中的s路径,java,url,httpconnection,Java,Url,Httpconnection,我有一个向接口发送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(HttpU

我有一个向接口发送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的另一台服务器上也可以正常工作。唯一的区别是两台服务器之间的Java1.7和1.8。那么,这是java 1.8中的一项新功能吗?

HTTP 1.1是在中定义的。标题在第4.2节中定义,第3.2节定义了标题,第3.2节本身将标题字段名称定义为

field-name  =  1*<any CHAR, excluding CTLs, SPACE, and ":">
然而,举例来说,它确实如此


我不确定您在哪个版本的java 8上运行此操作,因为在for OpenJDK 8中,行号与您的异常不一致,而且OpenJDK 8似乎也没有该检查。

那么我应该如何处理它?您使用该标题的目的是什么?这是一些非常古老的代码,但是我对java的知识有限,我认为它正在设置URL值来发送web服务请求。我可以评论httpconn.setRequestProperty(smsService,smsService);如果它没有意义?除非API明确要求它(我对此表示怀疑),否则您必须删除该行,是的。删除该行后它工作正常,非常感谢。我现在很好奇,同样的代码是如何在JDK1.7上的另一台服务器上工作的
private void checkMessageHeader(String key, String value) {
        char LF = '\n';
        int index = key.indexOf(LF);
        if (index != -1) {
            throw new IllegalArgumentException(
                "Illegal character(s) in message header field: " + key);
        }

        else {
            if (value == null) {
                return;
            }

            index = value.indexOf(LF);
            while (index != -1) {
                index++;
                if (index < value.length()) {
                    char c = value.charAt(index);
                    if ((c==' ') || (c=='\t')) {
                        // ok, check the next occurrence
                        index = value.indexOf(LF, index);
                        continue;

                    }
                }

                throw new IllegalArgumentException(
                    "Illegal character(s) in message header value: " + value);

            }
        }
    }
private void checkMessageHeader(String key, String value) {
    char LF = '\n';
    int index = key.indexOf(LF);
    int index1 = key.indexOf(':');
    if (index != -1 || index1 != -1) {
        throw new IllegalArgumentException(
            "Illegal character(s) in message header field: " + key);
    }
    // ... rest of the method ...