0

Here is the code i am using the open the URLConnection to another webapllication

try {   
 URL   url = new URL("https://mySecondWebApp/Cart");
 HttpsURLConnection conn1 = (HttpsURLConnection) url.openConnection();//line1
 conn1 .connect();//line 2 does not establishes the connection
 conn1.getInputStream();// line 3 works which means that i get control to my app2 in debugger.But this also passes the control to IOException stating error java.io.FileNotFoundException: https://mySecondWebApp/Cart

 }
  catch (MalformedURLException e) {
    log.error("MalformedURLException" + e);

  }
  catch (IOException e) {

  log.info("getting IO error");
  }

I am not getting why line 2 does not establish the connection with app2 while line 3 does? second thing is why i get the file notfound exception after line 3 even though it sucessfully connect to app2 which i want to avoid.My intention is just to establish the connection to app2 so that i get control inside my java code of app2

M Sach
  • 33,416
  • 76
  • 221
  • 314
  • Try checking code of HttpsURLConnection, or go through API doc, you will get the answer – jmj Jul 26 '12 at 06:51
  • As per docs.oracle.com/javase/tutorial/networking/urls/connecting.html, looks like both connect and get inputstream should make the connection to app2 .See the line You are not always required to explicitly... in the link.But in my case only getinputstream is doing that?Not sure why? Also not getting why i get filenotfound exception when i do conn1.getInputStream() – M Sach Jul 26 '12 at 07:45

2 Answers2

0

The method name is probably the source of your confusion. openConnection() method does not establish a connection, it just prepares the connection object. here is the link to the API for the method:

URL.openConnection()

the following is written there: "It should be noted that a URLConnection instance does not establish the actual network connection on creation. This will happen only when calling URLConnection.connect()."

EDIT: I have confused the lines (Thanks Ankit) and therefore the above is not relevant. Connecet does establish the connection but actually it is not needed here as the method getInputStream will establish the connection if it was not established already.

  • But URLConnection.connect() also does not establishes the conn1.getInputStream() does establish the connection but problem is i also get error i.e java.io.FileNotFoundException: https://mySecondWebApp/Cart after conn1.getInputStream() gets executed – M Sach Jul 26 '12 at 06:38
  • @A.J.: I am not getting why line 2 does not establish the connection with app2 while line 3 does. and line 2 is conn1 .connect(); – Ankit Jul 26 '12 at 06:39
  • ok i finally found it :) please check the following post from @BalusC : http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests All I have written was fractions of what I remember from it. so Please Ignore my answer (and don't accept it please) and read this as it will most probably explain everything you might want to know at this stage. –  Jul 26 '12 at 07:13
  • @A.J. conn1.getInputStream() does establish the connection successfully and executes the code on app2 but problem is i also get error which is java.io.FileNotFoundException: mySecondWebApp/Cart after conn1.getInputStream() gets executed . I do not know how to get rid of this and why i am getting this – M Sach Jul 26 '12 at 07:37
  • 1
    it seems there is an error returned by the server and therefore when you call getInputStream you are getting the file not found. try the following: comment out the line with getInputStream. keep the one with connect(). the add the following after it to see the status returned by the server: int status = conn1.getResponseCode(); what code do you get? –  Jul 26 '12 at 07:55
  • if you do what i mentioned in the previous comment and you get status code of some error, you can try to read the error stream to see if there is more info : InputStream is = conn1.getErrorStream(); –  Jul 26 '12 at 09:00
  • i get the error code as 404. but good thing is that with conn1.getResponseCode() i was able to connect to app2 and also i did not get any filenotfound exception. so i can manage it somehow. But still i am not able to understand why i am getting 404 error when i am able to connect to app2 successfully – M Sach Jul 26 '12 at 16:59
  • i mentioned in another comment to check the error stream returned as it might give you info on what is wrong: InputStream is = conn1.getErrorStream(); –  Jul 26 '12 at 21:21
0

The connect() method is not expected to do this task, from javadoc:

public abstract void connect()
                      throws IOException

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established. If the connect method is called when the connection has already been opened (indicated by the connected field having the value true), the call is ignored. URLConnection objects go through two phases: first they are created, then they are connected. After being created, and before being connected, various options can be specified (e.g., doInput and UseCaches). After connecting, it is an error to try to set them. Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.

Ankit
  • 3,083
  • 7
  • 35
  • 59
  • from the BOLD lines: "first they are created" and that is with openConnection. "then they are connected" and that is with connect() or other methods that make connect implicitly like getInputStream or getContentLength. –  Jul 26 '12 at 07:01
  • Ankit As per http://docs.oracle.com/javase/tutorial/networking/urls/connecting.html, looks like both connect and get inputstream should make the connection to app2 .See the line You are not always required to explicitly... in the link.But in my case only getinputstream is doing that?Not sure why? Any ways can you provide your input my last comment on A.J. If i dont get filenotfound exception, then my life becomes easy – M Sach Jul 26 '12 at 07:43
  • Try to return something from your app2 URL, that you are trying to call, return response.getWriter.write("write something");. Another (bad) way to do it is by having catch block for your specific Exception and then do nothing in that block. – Ankit Jul 26 '12 at 12:43