0

First off, I apologise as I'm almost sure this question is a duplicate. I have a very simple/basic question which I'm sure must have been answered somewhere... but I cant seem to find the answer anywhere :)

I'm working on an application that sends requests to a server via HTTP in batch mode. The application is executed overnight and may send approx 1 million requests per hour over a 6-8 hour period. These are not web requests, the response from the server will be a JSON string. Every request is sent to the same server/port, but every request can have different parameters.

The question I have is how to create the connection only once, but pass different parameters on each each call/execution. I've seen How to use java.net.URLConnection to fire and handle HTTP requests? and lots of tutorials across the web but I didn't quite understand how to address my particular need. I've also looked into the Java documentation for the HTTP URLConnection and URLEncoder but I could not find a way to do what I need using either.

The code I have is:

//Build query string
String url = "http://example.com:5000/someScript?foo=bar&ya=hoo";

//Open a connection
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");

//send an HTTP GET request and receive back a response
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
//... do something with the response

While this works, I find that the (HttpURLConnection) obj.openConnection() line takes an average of approx. 85 million nanoseconds for each call. Over a 6 hour period, that adds up to quite a bit. This isn't a blocker but I'm still really hoping to improve the performance.

Ideally, I'd like to do something like the below:

This code is called only once (or once per thread perhaps):

//URL of service
String url = "http://example.com:5000/someScript?";

//Open a connection
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");

This code would then be called for the 1st request: (the 1st line is pseudocode)

//set query parameter -- THIS IS NOT A REAL METHOD OF COURSE
con.setQueryParameter("foo=bar&ya=hoo");

//send an HTTP GET request and receive back a response
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
//... do something with the response

And then this code would be called for the 2nd request: (the 1st line is pseudocode)

//set query parameter -- THIS IS NOT A REAL METHOD OF COURSE
con.setQueryParameter("foo=blah&ya=nope");

//send an HTTP GET request and receive back a response
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
//... do something with the response

And so on and so forth. I hope my question is clear enough. Any help would be appreciated.

Thanks!

Sangam
  • 51
  • 5

0 Answers0