我正在用 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 );