在 HTTP 请求正文中发送数据失败

发布于 2024-07-19 06:23:33 字数 1189 浏览 4 评论 0原文

我在 BlackBerry 应用程序中使用 J2ME 的 HttpConnection 类将数据发送到 Web 服务器。 我需要在 HTTP 请求正文中发送图像的内容。

这就是我所做的

  1. 获取数组中文件的字节

  2. 打开HTTP连接

  3. 设置内容类型标头as image/jpeg

  4. 获取连接的输出流

  5. 将字节写入输出流

  6. 关闭输出流和连接

但图像未上传到服务器。 可能是什么问题呢?

谢谢。

编辑 - 添加代码

HttpConnection conn = null;
OutputStream out = null;

try{
    conn = new HttpConnection(Connector.open(myURL));
    conn.setRequestProperty("Content-Type", "image/jpeg");
    conn.setRequestMethod(HttpConnection.POST);
    conn.setRequestProperty("Content-Disposition", "form-data");
    conn.setRequestProperty("Connection", "Keep-Alive");

    out = conn.openOutputStream;
    out.write(buffer, 0, buffer.length);
    conn.setRequestProperty("Content-Length", buffer.length);
    out.flush();
}
catch(Exception e){
    e.printStackTrace();
}
finally{
    if(out != null)
        out.close();

    if(conn != null){
        System.out.println("" + conn.getResponseCode());
        conn.close();
    }
}

编辑

相同的代码,当我尝试使用字符串时,工作正常并将字符串发送到服务器。 但仍然是图像字节的问题。

I am using the HttpConnection class of J2ME in my BlackBerry app to send data to a web server. I need to send the contents of an image in the body of the HTTP request.

This is what I do

  1. Get the bytes of the file in an array

  2. Open HTTP connection

  3. Set content type header as image/jpeg

  4. Get output stream of the connection

  5. Write the bytes to the output stream

  6. Close the output stream and connection

But the image is not uploaded to the server. What could be the problem?

Thanks.

EDIT - Adding code

HttpConnection conn = null;
OutputStream out = null;

try{
    conn = new HttpConnection(Connector.open(myURL));
    conn.setRequestProperty("Content-Type", "image/jpeg");
    conn.setRequestMethod(HttpConnection.POST);
    conn.setRequestProperty("Content-Disposition", "form-data");
    conn.setRequestProperty("Connection", "Keep-Alive");

    out = conn.openOutputStream;
    out.write(buffer, 0, buffer.length);
    conn.setRequestProperty("Content-Length", buffer.length);
    out.flush();
}
catch(Exception e){
    e.printStackTrace();
}
finally{
    if(out != null)
        out.close();

    if(conn != null){
        System.out.println("" + conn.getResponseCode());
        conn.close();
    }
}

EDIT

The same code, when I try it with a string, works fine and sends the string to the server. But it is still a problem with the image bytes.

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

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

发布评论

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

评论(4

哆兒滾 2024-07-26 06:23:33

您的列表中可能缺少一些内容:

  • 在 2 和 3 之间使用 HttpConnection.setRequestMethod(HttpConnection.POST)。
  • 使用 HttpConnection.setRequestProperty("Content-Length",...) 在 5 和 6 之间设置内容长度
  • 。 HTTP 请求响应代码可以帮助调试您的问题:在关闭 OutputStream 之后、关闭 HttpConnection 之前调用 HttpConnection.getResponseCode()。

A few things that may be missing from your list:

  • use HttpConnection.setRequestMethod(HttpConnection.POST) between 2 and 3.
  • set content length with HttpConnection.setRequestProperty("Content-Length",...) between 5 and 6.
  • knowing the HTTP request response code can help debug your issues: call HttpConnection.getResponseCode() after you've closed the OutputStream but before you close the HttpConnection.
心碎的声音 2024-07-26 06:23:33

conn = new HttpConnection(Connector.open(myURL));

这条线是错误的。 Connection 是一个工厂类,它通过查看适当的协议实现来创建新的 Connection 对象。

HttpConnection conn = (HttpConnection) Connector.open(myURL);

其余代码看起来没问题。 当您发布时,您至少需要定义内容类型和内容长度。

conn = new HttpConnection(Connector.open(myURL));

This line is wrong. Connection is a factory class that creates new Connection objects by looking it's appropriate protocol implementation.

HttpConnection conn = (HttpConnection) Connector.open(myURL);

The rest of the code seems ok. When you're POSTing, at minimun you need to define the content-type and content-length.

浅沫记忆 2024-07-26 06:23:33

您绝对需要在发送 POST 数据之前设置所有标头,包括 Content-Length 标头。

另外,请确保您发送的标头对请求有效,而不是仅响应的标头。

You most definitely need to set all headers before sending the POST data, including the Content-Length header.

Also, make sure you are sending headers valid for requests, and not response-only headers.

下雨或天晴 2024-07-26 06:23:33

您需要对字节进行编码(最好是 Base-64)并发送该字符串。 原始字节不太可能是 http 安全的。

然后在服务器上,您需要将它们解码回字节数组并执行您要使用它执行的任何操作(另存为文件、填充到数据库中等)

You'll need to encode the bytes (preferably Base-64) and send that string. The raw bytes aren't likely to be http safe.

Then on the server, you'll need to decode them back into a byte array and do whatever you were going to do with it (save as file, stuff into database, etc.)

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