将发布数据从一个 java servlet 写入另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/96360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:15:32  来源:igfitidea点击:

Writing post data from one java servlet to another

javaservlets

提问by denny

I am trying to write a servlet that will send a XML file (xml formatted string) to another servlet via a POST. (Non essential xml generating code replaced with "Hello there")

我正在尝试编写一个 servlet,它将通过 POST 将 XML 文件(xml 格式的字符串)发送到另一个 servlet。(非必要的 xml 生成代码替换为“Hello there”)

   StringBuilder sb=  new StringBuilder();
    sb.append("Hello there");

    URL url = new URL("theservlet's URL");
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();                
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Length", "" + sb.length());

    OutputStreamWriter outputWriter = new OutputStreamWriter(connection.getOutputStream());
    outputWriter.write(sb.toString());
    outputWriter.flush();
    outputWriter.close();

This is causing a server error, and the second servlet is never invoked.

这会导致服务器错误,并且永远不会调用第二个 servlet。

采纳答案by Peter Hilton

This kind of thing is much easier using a library like HttpClient. There's even a post XML code example:

使用像HttpClient这样的库,这种事情要容易得多。甚至还有一个后 XML 代码示例

PostMethod post = new PostMethod(url);
RequestEntity entity = new FileRequestEntity(inputFile, "text/xml; charset=ISO-8859-1");
post.setRequestEntity(entity);
HttpClient httpclient = new HttpClient();
int result = httpclient.executeMethod(post);

回答by Craig B.

Don't forget to use:

不要忘记使用:

connection.setDoOutput( true)

if you intend on sending output.

如果您打算发送输出。

回答by Sietse

I recommend using Apache HTTPClientinstead, because it's a nicer API.

我建议改用 Apache HTTPClient,因为它是一个更好的 API。

But to solve this current problem: try calling connection.setDoOutput(true);after you open the connection.

但是要解决当前的这个问题:connection.setDoOutput(true);打开连接后尝试调用。

StringBuilder sb=  new StringBuilder();
sb.append("Hello there");

URL url = new URL("theservlet's URL");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();                
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Length", "" + sb.length());

OutputStreamWriter outputWriter = new OutputStreamWriter(connection.getOutputStream());
outputWriter.write(sb.toString());
outputWriter.flush();
outputWriter.close();

回答by Josh

The contents of an HTTP post upload stream and the mechanics of it don't seem to be what you are expecting them to be. You cannot just write a file as the post content, because POST has very specific RFC standards on how the data included in a POST request is supposed to be sent. It is not just the formatted of the content itself, but it is also the mechanic of how it is "written" to the outputstream. Alot of the time POST is now written in chunks. If you look at the source code of Apache's HTTPClient you will see how it writes the chunks.

HTTP 后上传流的内容及其机制似乎与您期望的不同。你不能只写一个文件作为发布内容,因为 POST 有非常具体的 RFC 标准,关于如何发送包含在 POST 请求中的数据。它不仅是内容本身的格式,而且还是将内容“写入”到输出流的机制。现在很多时候 POST 都是分块写的。如果您查看 Apache 的 HTTPClient 的源代码,您将看到它如何写入块。

There are quirks with the content length as result, because the content length is increased by a small number identifying the chunk and a random small sequence of characters that delimits each chunk as it is written over the stream. Look at some of the other methods described in newer Java versions of the HTTPURLConnection.

结果是内容长度有一些怪癖,因为内容长度增加了一个标识块的小数字和一个随机的小字符序列,当每个块写入流时,它会分隔每个块。查看 HTTPURLConnection 的较新 Java 版本中描述的其他一些方法。

http://java.sun.com/javase/6/docs/api/java/net/HttpURLConnection.html#setChunkedStreamingMode(int)

http://java.sun.com/javase/6/docs/api/java/net/HttpURLConnection.html#setChunkedStreamingMode(int)

If you don't know what you are doing and don't want to learn it, dealing with adding a dependency like Apache HTTPClient really does end up being much easier because it abstracts all the complexity and just works.

如果您不知道自己在做什么并且不想学习它,那么处理添加像 Apache HTTPClient 这样的依赖项确实会容易得多,因为它抽象了所有复杂性并且可以正常工作。