是否可以在 POST 命令之后忽略网络服务器的响应?
我正在用 JAVA 编写一个程序,用于将大量 XML 文档发布到特定的网址,此外还有大量与此问题稍微无关的其他数据处理。唯一的麻烦是,我预计将处理大约 90,000 条记录。 POST XML 文档时,每条记录大约需要 10 秒,其中 9 秒是在 POST 后接收服务器响应所用的时间。
我的问题是:有没有办法将数据发布到网络服务器,然后忽略服务器的响应以节省时间?
这是一段给我带来麻烦的代码片段,根据系统计时器,从“writer.close”到“con.getResponseCode()”大约需要 9 秒
URL url = new URL(TargetURL);
con = (HttpsURLConnection) url.openConnection();
//Login with given credentials
String login = (Username)+":"+(Password);
String encoding = new sun.misc.BASE64Encoder().encode(login.getBytes());
con.setRequestProperty ("Authorization", "Basic " + encoding);
// specify that we will send output and accept input
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setConnectTimeout(20000) ; // long timeout, but not infinite
con.setReadTimeout(20000);
con.setUseCaches (false);
con.setDefaultUseCaches (false);
// tell the web server what we are sending
con.setRequestProperty ( "Content-Type", "text/xml" );
OutputStreamWriter writer = new OutputStreamWriter( con.getOutputStream() );
writer.write(data);
writer.flush();
writer.close();
//****This is our problem.*****//
int result = con.getResponseCode();
System.err.println( "\nResponse from server after POST:\n" + result );
I am writing a program in JAVA to POST a large number of XML Documents to a specific web address, in addition to a great deal of other data handling that is slightly unrelated to this question. The only trouble is, I'm expect to handle approximately 90,000 records. When POSTing an XML document, each record takes approximately 10 seconds, 9 of which is taken by receiving the response from the sever after POST.
My question is: Is there a way to POST data to a webserver, then ignore the server's response to save time?
Here is a snip of code that's giving me trouble, it takes approximate 9 seconds according to the system timer to go from "writer.close" to "con.getResponseCode()"
URL url = new URL(TargetURL);
con = (HttpsURLConnection) url.openConnection();
//Login with given credentials
String login = (Username)+":"+(Password);
String encoding = new sun.misc.BASE64Encoder().encode(login.getBytes());
con.setRequestProperty ("Authorization", "Basic " + encoding);
// specify that we will send output and accept input
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setConnectTimeout(20000) ; // long timeout, but not infinite
con.setReadTimeout(20000);
con.setUseCaches (false);
con.setDefaultUseCaches (false);
// tell the web server what we are sending
con.setRequestProperty ( "Content-Type", "text/xml" );
OutputStreamWriter writer = new OutputStreamWriter( con.getOutputStream() );
writer.write(data);
writer.flush();
writer.close();
//****This is our problem.*****//
int result = con.getResponseCode();
System.err.println( "\nResponse from server after POST:\n" + result );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到你的问题了。
使用仅读取标头的策略对您不起作用,因为问题不是由于服务器作为响应发送的大量数据造成的。问题是服务器需要很长时间来处理客户端发送的数据,因此即使发送一个短的确认响应也需要很长时间。
您要求的是异步响应。答案是 AJAX,我的首选是 GWT。
GWT 提供了三种与服务器执行异步通信的方法。
请阅读我的描述
但是,您可能更喜欢使用 JQuery,我对它的了解很少稀缺的熟悉度。
I see your problem.
Using the strategy to read only the header would not work for you because the problem is not due to voluminous amount of data the server is sending as a response. The problem is that the server takes a long to time to process the data your client had sent and therefore takes a long time to even send a short ack response.
What you are asking for is Asynchronous response. The answer is AJAX and my preference of choice is GWT.
GWT presents three ways to perform async communication with the server.
Please read my description at
But then, you might prefer to use JQuery, with which I have scant and scarce familiarity.
我宁愿使用 Apache HttpComponents。它允许您不读取响应正文,而只读取您明显需要的标头。
http://hc.apache.org/httpcomponents-client -ga/tutorial/html/fundamentals.html#d4e143
文档的这一部分有一个仅读取响应的几个字节的示例。
I'd rather use Apache HttpComponents. It lets you not read the response body, and only the headers which you obviously need.
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d4e143
That part of the docs has an example of only reading a few bytes of the response.