我用java编写简单的telnet应用程序,这个应用程序发送请求(xml)和读取响应。但是当我发送超过1200字节的请求(来自java)时,我得到HTTP 500.如果我从命令行发送此命令,我得到HTTP 200(确定)。 java中有最大大小限制吗?
我的代码:
public static void main(String[] args) throws IOException {
Socket pingSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
pingSocket = new Socket("host", 4380);
out = new PrintWriter(pingSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(pingSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
return;
}
String newLine = "\n";
String header = "POST /path HTTP/1.1" + newLine
+ "Host: host:4380" + newLine
+ "Authorization: Basic cuyasdyq123ha123" + newLine
+ "Connection: close" + newLine
+ "SOAPAction: \"\"" + newLine
+ "Content-Type: text/xml; charset=utf-8" + newLine
+ "Content-Length: 2000" + newLine;
String request = "my xml data here";
System.out.println(header + request);
out.println(header);
out.println(request);
System.out.println(in.readLine());
System.out.println();
while (in.read() != -1) {
System.out.print((char) in.read());
}
out.close();
in.close();
pingSocket.close();
}
感谢您的帮助
答案 0 :(得分:1)
这看起来像编码问题。我不知道你从命令行得到什么编码。如果你正在使用Windows,很可能是cp-1252。因此,您的1200个字符可能会达到1200个字节,这符合您指定的2000内容长度。
但在Java中,每个字符都表示为两个字节。所以你的1200个字符变成2400字节;但标题中的Content-Length:2000会使您的内容过早被切断。