2

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 );
cublie
  • 21
  • 2
  • How about using a different thread to get the response? Or more simply using a thread pool to send and receive many requests in parallel. – toto2 Jun 14 '11 at 23:36

2 Answers2

1

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.

  • GWT RPC
  • RequestBuilder
  • javascript include
  • MVP ClientFactory/EventBus

Please read my description at

But then, you might prefer to use JQuery, with which I have scant and scarce familiarity.

Blessed Geek
  • 21,058
  • 23
  • 106
  • 176
0

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.

k_b
  • 2,470
  • 18
  • 9