JohnnyProd
Technical User
I'm trying to write a small app which will measure the resposen time of our application. To do this I have created a serries of Get() and Post() methods which grab the time in two places, take the difference, and write the info out to a file. What I am now wondering is if there is a better way for me to be doing POSTs and GETs. This is a example off my POST method:
public String post(String myURL, String[][] nameValuePairs){
String response = "";
try
{
String startDate = new Date().toString();
long startTime = System.currentTimeMillis();
String urlName = rootURL + myURL;
String postData ="";
BufferedReader in;
URLConnection connection;
URL homeURL = new URL(urlName);
connection = homeURL.openConnection();
connection.setDoInput( true );
connection.setDoOutput( true );
connection.setUseCaches( false );
connection.setRequestProperty("Accept", "*/*"
;
connection.setRequestProperty("Referer", this.referer);
connection.setRequestProperty("Accept-Language", "en-us"
;
connection.setRequestProperty("User-Agent", ""
;
connection.setRequestProperty("content-type", "application/x- connection.setRequestProperty("Cookie", this.cookie);
((HttpURLConnection)connection).setRequestMethod("POST"
;
PrintWriter pw = new PrintWriter(connection.getOutputStream() );
for (int i = 0; i < nameValuePairs.length; i++)
{
postData += (nameValuePairs[0] + "="
;
for (int j = 1; j < nameValuePairs.length; j++)
{
postData += (nameValuePairs[j] + "&"
;
}
}
pw.write((postData));
pw.close();
String tmpString = "";
try
{
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((tmpString = in.readLine()) != null)
{
response += tmpString + '\n';
}
in.close();
} catch(FileNotFoundException myFileNotFoundException){
myFileNotFoundException.printStackTrace(System.out);
}
if ((connection.getHeaderField(7) != null) && (connection.getHeaderFieldKey(7).compareTo("Set-Cookie"
== 0))
{
this.cookie = connection.getHeaderField(7);
}
long responseTime = (System.currentTimeMillis() - startTime);
...
Using this method for POSTing, I can't really determin when the request os being sent and at what exact point the response has been finished. Any ideas people? Is there a better way for me to be going about this?
public String post(String myURL, String[][] nameValuePairs){
String response = "";
try
{
String startDate = new Date().toString();
long startTime = System.currentTimeMillis();
String urlName = rootURL + myURL;
String postData ="";
BufferedReader in;
URLConnection connection;
URL homeURL = new URL(urlName);
connection = homeURL.openConnection();
connection.setDoInput( true );
connection.setDoOutput( true );
connection.setUseCaches( false );
connection.setRequestProperty("Accept", "*/*"
connection.setRequestProperty("Referer", this.referer);
connection.setRequestProperty("Accept-Language", "en-us"
connection.setRequestProperty("User-Agent", ""
connection.setRequestProperty("content-type", "application/x- connection.setRequestProperty("Cookie", this.cookie);
((HttpURLConnection)connection).setRequestMethod("POST"
PrintWriter pw = new PrintWriter(connection.getOutputStream() );
for (int i = 0; i < nameValuePairs.length; i++)
{
postData += (nameValuePairs[0] + "="
for (int j = 1; j < nameValuePairs.length; j++)
{
postData += (nameValuePairs[j] + "&"
}
}
pw.write((postData));
pw.close();
String tmpString = "";
try
{
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((tmpString = in.readLine()) != null)
{
response += tmpString + '\n';
}
in.close();
} catch(FileNotFoundException myFileNotFoundException){
myFileNotFoundException.printStackTrace(System.out);
}
if ((connection.getHeaderField(7) != null) && (connection.getHeaderFieldKey(7).compareTo("Set-Cookie"
{
this.cookie = connection.getHeaderField(7);
}
long responseTime = (System.currentTimeMillis() - startTime);
...
Using this method for POSTing, I can't really determin when the request os being sent and at what exact point the response has been finished. Any ideas people? Is there a better way for me to be going about this?