Results 1 to 2 of 2
- 01-02-2013, 04:49 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 1
- Rep Power
- 0
Http Request sending to an HTTPS endpoint
Hi,
i have a java program that sends a SOAP request to a parameterised URL (Endpoint)
what my problem is, if i send it to an HTTP endpoint it completes this in hardly any time at all, however if i send it over https it takes much longer.
has anyone any suggestions as to why this could be?
Thanks very much.
James
Program is as follows:
Java Code:package PickToWebServiceAndBack_2; / * A small program to send a payload to a web service via HTTP or HTTPS. It receives the payload, url and soap action from the command line: * args[0] - web service url * args[1] - soap action * args[2] - request timeout in milliseconds * args[3] - payload * args[4] - new line character replacement, if null then char(10) is used. this only does anything if the response comes back on multiple lines * args[5] - log file path. this must already exist * args[6] - log file name. this should be unique per branch box. will go into args[5] * * The program may throw one of the following exceptions: * java.io.IOException - for problems with io streams and log files * java.lang.ArrayIndexOutOfBoundsException - not enough args passed in on the cmd line * java.lang.NumberFormatException - the value passed in for timeout is not a number * java.lang.IllegalArgumentException - a parameter contains an illegal character * java.net.SocketTimeoutException - the connection times out * java.net.UnknownHostException - the url is not recognised * javax.net.ssl.SSLException - ssl certificate problems * javax.net.ssl.SSLPeerUnverifiedException - ssl certificate problems * javax.xml.transform.TransformerException - web service response is not valid xml * org.apache.http.client.ClientProtocolException - the protocol used by the url is not recognised * org.apache.http.conn.ConnectTimeoutException - the connection times out * org.apache.http.conn.HttpHostConnectException - for when the connection is refused * java.lang.Exception - anything else that might go wrong * * These will be output to the command line in a user friendly form while the actual error and message will be logged after which execution will cease. * * The program will output the response from the web service on the command line neatly formatted if args[4] is empty, otherwise potential char(10)s * will be replaced with the character as returned from char(args[4]). * * All operations performed in this program will be logged to a file specified in args[5] + args[6]. */ import java.io.BufferedReader; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; import java.net.SocketException; import java.net.SocketTimeoutException; import java.net.UnknownHostException; import java.nio.charset.Charset; import javax.net.ssl.SSLException; import javax.net.ssl.SSLPeerUnverifiedException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.HttpHostConnectException; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; public class Main { private static String log_error_message = ""; private static String user_error_message = ""; private static Exception user_exception; private static FileWriter log_file; private static PrintWriter log_file_out; /* * reads args to send an xml packet to a web service * makes the response pretty * logs all operations and errors * presents runtime errors in a user friendly manner */ public static void main(String[] args) { long ws_start_time = 0; long ws_send_time = 0; long ws_response_time = 0; long ws_end_time = 0; long total_start_time = System.currentTimeMillis(); long total_end_time = 0; long log_created_time = 0; long response_processed_time = 0; try { String f_path = args.length < 6? "": args[5]; String f_name = args.length < 7? "temp_log": args[6]; InitLogFile(f_path, f_name); log_created_time = System.currentTimeMillis(); char new_line = 10; String ws_url = args[0]; String ws_soap_action = args[1]; int timeout = Integer.parseInt(args[2]); String ws_request = args[3]; if (args[4].compareTo("") != 0) { new_line = (char)Integer.parseInt(args[4]); } log_file_out.println("Web Service URL (args[0]) : " + args[0]); log_file_out.println("SOAP Action (args[1]) : " + args[1]); log_file_out.println("Request timeout (args[2]) : " + args[2]); log_file_out.println("Request XML (args[3]) : " + args[3]); log_file_out.println("Replacement new line char (args[4]) : " + args[4]); log_file_out.println("Log file path (args[5]) : " + args[5]); log_file_out.println("Log file name (args[6]) : " + args[6]); byte[] b = ws_request.getBytes(); log_file_out.println("Payload size (bytes) " + String.valueOf(b.length)); log_file_out.println("Preparing to send request to " + ws_url); ws_start_time = System.currentTimeMillis(); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, timeout); //timeout for establishing the intial connection HttpConnectionParams.setSoTimeout(httpParams, timeout); //timeout for response DefaultHttpClient client = new DefaultHttpClient(httpParams); HttpPost post = new HttpPost(ws_url); post.setParams(httpParams); if (ws_soap_action.length() > 0) { post.setHeader("SOAPAction", ws_soap_action); } post.setHeader("Content-Type", "text/xml"); StringEntity outgoing = new StringEntity(ws_request, Charset.forName("UTF-8")); outgoing.setContentType("text/xml"); post.setEntity(outgoing); // get response log_file_out.println("Sending request XML to " + ws_url); ws_send_time = System.currentTimeMillis(); HttpResponse response = client.execute(post); ws_response_time = System.currentTimeMillis(); log_file_out.println("Received response from " + ws_url); ws_end_time = System.currentTimeMillis(); HttpEntity entity = response.getEntity(); InputStreamReader isr = new InputStreamReader(entity.getContent()); BufferedReader br = new BufferedReader(isr); String inputLine; String ws_response = ""; log_file_out.println("Reading web service response"); while ((inputLine = br.readLine()) != null) { ws_response = ws_response + inputLine; } log_file_out.println("Response: " + ws_response); log_file_out.println("Formatting web service response"); ws_response = ws_response.replaceAll(">\\s*<", "><"); ws_response = prettyFormat(ws_response, 1); ws_response = ws_response.replaceAll("\\r\\n", Character.toString(new_line)); ws_response = ws_response.trim(); log_file_out.println("Formatted response:"); log_file_out.println(ws_response); log_file_out.println("Closing input stream"); isr.close(); br.close(); log_file_out.println("Closing connection to " + ws_url); post.releaseConnection(); System.out.println(ws_response); response_processed_time = System.currentTimeMillis(); } catch (ArrayIndexOutOfBoundsException ex) { user_error_message += "Not enough arguments were passed to PickToWebServiceAndBack_2.jar. "; log_error_message += ex.toString(); user_exception = ex; } catch (ClientProtocolException ex) { user_error_message += "The URL is invalid. "; log_error_message += ex.toString(); user_exception = ex; } catch (SSLPeerUnverifiedException ex) { user_error_message += "There is a problem with the security certificate for this web service. "; log_error_message += ex.toString(); user_exception = ex; } catch (SSLException ex) { user_error_message += "There is a problem with the security certificate for this web service. "; log_error_message += ex.toString(); user_exception = ex; } catch (HttpHostConnectException ex) { user_error_message += "There is a problem connecting to the web service host. "; log_error_message += ex.toString(); user_exception = ex; } catch (NumberFormatException ex) { user_error_message += "The timeout value given is not a number. "; log_error_message += ex.toString(); user_exception = ex; } catch (IllegalArgumentException ex) { user_error_message += "There was a problem with one of the parameters. "; log_error_message += ex.toString(); user_exception = ex; } catch (ConnectTimeoutException ex) { user_error_message += "The connection timed out. "; log_error_message += ex.toString(); user_exception = ex; } catch (SocketException ex) { user_error_message += "The connection is closed. "; log_error_message += ex.toString(); user_exception = ex; } catch (SocketTimeoutException ex) { user_error_message += "The connection timed out. "; log_error_message += ex.toString(); user_exception = ex; } catch (TransformerException ex) { user_error_message += "There was a problem formatting the web service response. "; log_error_message += ex.toString(); user_exception = ex; } catch (UnknownHostException ex) { user_error_message += "The URL cannot be reached. "; log_error_message += ex.toString(); user_exception = ex; } catch (IOException ex) { if (ex.toString().toLowerCase().contains("http response code:")) { user_error_message += "The server has rejected the request. "; } else { user_error_message += "There was a problem with the log file. "; } log_error_message += ex.toString(); user_exception = ex; } catch (Exception ex) { user_error_message += "An unexpected error occurred. "; log_error_message += ex.toString(); user_exception = ex; } finally { if (user_error_message.compareTo("") != 0) { user_error_message = "ERROR: " + user_error_message + " Contact the Service Desk."; System.out.println(user_error_message); log_file_out.println("Error reported to user: " + user_error_message); log_file_out.println("Actual error: " + log_error_message); user_exception.printStackTrace(log_file_out); } else { log_file_out.println("No Java runtime errors"); } total_end_time = System.currentTimeMillis(); String str = ""; str = String.format("Log file created in %7d ms", (log_created_time - total_start_time)); log_file_out.println(str); str = String.format("Parameters checked in %7d ms", (ws_start_time - log_created_time)); log_file_out.println(str); str = String.format("Received payload from web service in %7d ms", (ws_response_time - ws_send_time)); log_file_out.println(str); str = String.format("Total web service time %7d ms", (ws_end_time - ws_start_time)); log_file_out.println(str); str = String.format("Web service response processed in %7d ms", (response_processed_time - ws_end_time)); log_file_out.println(str); str = String.format("Total time to execute Java program %7d ms", (total_end_time - total_start_time)); log_file_out.println(str); log_file_out.println("Finished"); //close log file streams log_file_out.flush(); log_file_out.close(); try { log_file.flush(); log_file.close(); } catch (IOException ex) { } } } /* * creates a filewriter and corresponding printwriter to log operations at the given path with the given name * if the path is empty then throws ioexception * if the name is empty then uses temp_log instead */ private static void InitLogFile(String f_path, String f_name) throws IOException { boolean append = false; if (f_path.trim().compareTo("") == 0) { throw new IOException(); } else { if (!f_path.endsWith("/")) f_path.concat("/"); } if (f_name.trim().compareTo("") == 0) { f_name = "temp_log"; append = true; } f_name = f_path.concat(f_name); File f = new File(f_name); if (!f.exists()) { f.createNewFile(); } f = null; log_file = new FileWriter(f_name, append); log_file_out = new PrintWriter(log_file); } /* * makes the xml string pretty */ public static String prettyFormat(String input, int indent) throws TransformerException { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } }
- 01-02-2013, 06:22 PM #2
Similar Threads
-
How to post HTTPS request
By dror in forum NetworkingReplies: 1Last Post: 07-29-2012, 01:34 PM -
Share session between http and https
By gnorro in forum Advanced JavaReplies: 1Last Post: 02-09-2011, 08:02 AM -
How redirect a request to https
By vns955 in forum Web FrameworksReplies: 1Last Post: 11-12-2009, 03:26 AM -
switching between HTTP and HTTPS
By mutuah in forum Advanced JavaReplies: 6Last Post: 08-03-2007, 10:08 PM -
How to post HTTPS request from java client to server
By Desai in forum NetworkingReplies: 1Last Post: 07-14-2007, 05:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks