我无法使用 Java 从 Telnet 发送超过 1200 字节的请求 {Closed}

发布于 2025-01-06 06:27:48 字数 1495 浏览 1 评论 0原文

我用java编写简单的telnet应用程序,该应用程序发送请求(xml)并读取响应。但是,当我(从 java)发送超过 1200 个字节的请求时,我会得到 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();
    }

感谢您的帮助

I write simple telnet application with java, this application send request (xml) and read response. But when I send request (from java) more than 1200 bytes then I get HTTP 500. If I send this command from command line I get HTTP 200 (Ok). Is there max size limit in java?

My Code:

    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();
    }

Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2025-01-13 06:27:48

这看起来像是编码问题。我不知道您从命令行获得的编码是什么。如果您使用的是 Windows,很可能是 cp-1252。因此,您的 1200 个字符可能会计算为 1200 个字节,这符合您指定的 2000 个内容长度。

但在Java中,每个字符都表示为两个字节。所以你的 1200 个字符变成了 2400 个字节;但标题中的 Content-Length: 2000 会使您的内容过早被切断。

This looks like an encoding issue. I don't know what encoding you're getting from your command line. Could well be cp-1252 if you're using windows. So your 1200 characters probably work out to 1200 bytes, which fits within the 2000 Content-Length that you've specified.

But in Java, every character is represented as two bytes. So your 1200 characters are turning into 2400 bytes; but the Content-Length: 2000 in the header is making your content cut off prematurely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文