为什么从 c# 和 java 发送的 http 内容不同?

发布于 2024-12-09 05:16:37 字数 1219 浏览 0 评论 0原文

我有一个 xml 文件,需要将其作为帖子发送到 REST 服务器。当我从 c# 和 java 读取完全相同的文件时,字节到达服务器时不匹配。 java 失败并出现 500 内部服务器错误,而 c# 则完美运行。服务器是c#的。

C# 中的文件读取方式如下:

using (ms = new MemoryStream())
{
    string fullPath = @"c:\pathtofile\datalast.xml";
    using (FileStream outStream = File.OpenRead(fullPath))
    {
        outStream.CopyTo(ms);
        outStream.Flush();

    }

    ms.Position = 0;
    var xmlDoc = new XmlDocument();
    xmlDoc.Load(ms);
    content = xmlDoc.OuterXml;

}

然后将内容发送到使用 HttpWebResponse 的调用。

java (Android) 代码如下读取文件:

FileInputStream fis = app.openFileInput(DATA_LAST_FILE_NAME);
byte[] buffer = new byte[1024];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len;
while ((len = fis.read(buffer)) != -1)
{
    outputStream.write(buffer, 0, len);

}

outputStream.close();
fis.close();
ByteArrayEntity data = new ByteArrayEntity(buffer);
data.setContentType("application/xml");
post.setEntity(data);
HttpResponse response = request.execute(post);

生成的数组的大部分是相同的。唯一的区别似乎在于前 3 个字节。 c# 字节数组的前 3 个值是:

239,187,191

java 的值是:

-17、-69、-65

这里发生了什么?我应该怎么办?

谢谢,

\^/生病了

I have an xml file that I need to send to a REST server as a post. When I read the exact same file from c# and java the bytes do not match when they arrive at the server. The java ones fail with a 500 Internal Server Error while the c# one works perfectly. The server is c#.

The file in c# is read as follows:

using (ms = new MemoryStream())
{
    string fullPath = @"c:\pathtofile\datalast.xml";
    using (FileStream outStream = File.OpenRead(fullPath))
    {
        outStream.CopyTo(ms);
        outStream.Flush();

    }

    ms.Position = 0;
    var xmlDoc = new XmlDocument();
    xmlDoc.Load(ms);
    content = xmlDoc.OuterXml;

}

content is then sent to a call that uses an HttpWebResponse

The java (Android) code reads the file like this:

FileInputStream fis = app.openFileInput(DATA_LAST_FILE_NAME);
byte[] buffer = new byte[1024];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len;
while ((len = fis.read(buffer)) != -1)
{
    outputStream.write(buffer, 0, len);

}

outputStream.close();
fis.close();
ByteArrayEntity data = new ByteArrayEntity(buffer);
data.setContentType("application/xml");
post.setEntity(data);
HttpResponse response = request.execute(post);

For the most part the arrays generated are identical. The only difference seems to be in the first 3 bytes. The c# byte array's first 3 values are:

239,187,191

The java ones are:

-17,-69,-65

What is happening here? What should I do?

Thanks,

\ ^ / i l l

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

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

发布评论

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

评论(2

乖乖兔^ω^ 2024-12-16 05:16:37

看看您在这里所做的事情:

FileInputStream fis = app.openFileInput(DATA_LAST_FILE_NAME);
byte[] buffer = new byte[1024];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len;
while ((len = fis.read(buffer)) != -1)
{
    outputStream.write(buffer, 0, len);

}

outputStream.close();
fis.close();
ByteArrayEntity data = new ByteArrayEntity(buffer);

您正在从读取数据时使用的缓冲区创建ByteArrayEntity。几乎可以肯定,它的长度不正确(它总是长度为 1024),而且很可能也不包含所有数据。

您应该使用您一直在写入的ByteArrayOutputStream,例如

ByteArrayEntity data = new ByteArrayEntity(outputStream.toByteArray());

(顺便说一下,您应该在finally块中关闭fis。 )

编辑:您打印到控制台的值确实只是显示有符号和无符号表示之间的差异。我相信,它们与 Java 代码失败的原因无关,这是由于上述问题造成的。您应该在 Wireshark 中查看通过线路发送的内容 - 这将告诉您到底发生了什么。

Look at what you're doing here:

FileInputStream fis = app.openFileInput(DATA_LAST_FILE_NAME);
byte[] buffer = new byte[1024];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len;
while ((len = fis.read(buffer)) != -1)
{
    outputStream.write(buffer, 0, len);

}

outputStream.close();
fis.close();
ByteArrayEntity data = new ByteArrayEntity(buffer);

You're creating the ByteArrayEntity from the buffer that you've used when reading the data. It's almost certainly not the right length (it will always be length 1024), and it may well not have all the data either.

You should be using the ByteArrayOutputStream you've been writing into, e.g.

ByteArrayEntity data = new ByteArrayEntity(outputStream.toByteArray());

(You should be closing fis in a finally block, by the way.)

EDIT: The values you've printed to the console are indeed just showing the differences between signed and unsigned representations. They have nothing to do with the reason the Java code is failing, which is due to the above problem, I believe. You should look at what's being sent over the wire in Wireshark - that'll show you what's really going on.

九公里浅绿 2024-12-16 05:16:37

看看这个: http://en.wikipedia.org/wiki/Byte_order_mark

编辑: java和C#之所以不同,是因为在读取字节时,C#是无符号的,而java是有符号的。然而,相同的二进制值。

Take a look at this: http://en.wikipedia.org/wiki/Byte_order_mark

EDIT: The reason why java and C# are different is that when reading the bytes, C# is unsigned, and java is signed. Same binary values, however.

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