Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpsUrlConnection - Facing problem while reading the input stream and establishing the cookie

 
sambasiva kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please find below the error that I am getting while reading the input stream.

The value of cookie in the process request is ............... null
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2155)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2621)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:734)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:251)
at com.orbitreasury.fixedincome.presentation.util.AppServCommunication.receiveRequest(AppServCommunication.java:227)
at com.orbitreasury.fixedincome.presentation.util.AppServCommunication.processRequest(AppServCommunication.java:166)
at com.orbitreasury.fixedincome.control.ServerResets.main(ServerResets.java:62)


Please help me.
Thanks in advance.
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you please make your question more descriptive..... we didn't get more idea what is your problem ??

Please also put some code what you use ?
 
sambasiva kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I tried by including the following to establish the cookie


<sun-web-app>
<session-config>
<session-manager persistence-type="memory">
<manager-properties>
<property name="reapIntervalSeconds" value="20" />
</manager-properties>
</session-manager>
<session-properties>
<property name="enableCookies" value="true" />
</session-properties>
<cookie-properties>
<property name="cookiePath" value="/" />
<property name="cookieMaxAgeSeconds" value="-1" />
</cookie-properties>
</session-config>
<jsp-config/>
</sun-web-app>

in the sun-web.xml.


Please find the code that I am using.
-------------------------------------------------------------------------------------------------------------------------------
public synchronized PageWrapper processRequest(PageWrapper pWrapper) {
Console.println("inside processRequest urlCurrentPage = "+FISConstants.urlCurrentPage);
if(FISConstants.urlCurrentPage == null) {
Console.println("url is null application is running");
// This Condition will be true if it is run as a application
try {
// Uncomment below 2 lines to run it as a application
// NavigationController aNavController = NavigationController.getInstance();
// pWrapper = aNavController.onEvent(pWrapper);
} catch (Exception e ) {
e.printStackTrace();
}
return pWrapper;
}
if(!connectServlet()) {
pWrapper.setStatus(false);
pWrapper.setErrorMessage("Could not connect to servlet");
return pWrapper;
}
sendRequest(pWrapper);
if(cookie == null) cookie = readCookie(connection);
System.out.println("The value of cookie in the process request is ............... "+ cookie);
return receiveRequest();
}


private boolean sendRequest(PageWrapper pWrapper) {
ObjectOutputStream out = null;
try {
connection.setDoInput(true);
connection.setDoOutput(true);
System.out.println("Inside sendRequest");
out = new ObjectOutputStream(connection.getOutputStream());
out.writeObject(pWrapper);
out.flush();
}
catch (Exception e) {
e.printStackTrace();
return false; //return false in case of Exception
}
finally {
try {
if(out != null)
out.close();
} catch(Exception e) {
Console.println("Unable to close Connection"+e);
}

}
return true;
}


private PageWrapper receiveRequest() {

PageWrapper pWrapper = null;
ObjectInputStream in = null;
try {
System.out.println("Inside receiveRequest");
in = new ObjectInputStream(connection.getInputStream());
System.out.println("The Object Input Stream is .............." + in);
pWrapper = (PageWrapper)in.readObject();
System.out.println("The page wrapper Object is .............." + pWrapper);

}
catch(Exception e) {
e.printStackTrace();
pWrapper = new PageWrapper();
pWrapper.setStatus(false);
pWrapper.setErrorMessage(FISConstants.ERROR_URL);
}
finally {
try {
if(in != null)
in.close();
} catch(Exception e) {
Console.println("Unable to close Connection");
}
return pWrapper;
}
}



private boolean connectServlet() {
try {
connection = (HttpsURLConnection)dataURL.openConnection();
System.out.println("Connecion in connectServlet is ................." + connection);
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);
//Console.println("URL is " + dataURL);
if( cookie != null ) connection.setRequestProperty(FISConstants.COOKIE,cookie);
}
catch (Exception e) {
e.printStackTrace();
return false; //In case of Exception returns false
}
return true;
}

private String readCookie(URLConnection con) {
String key;
String value;
for( int i = 1; ( key = con.getHeaderFieldKey( i ) )!= null; ++i ){
key = key.toLowerCase();
Console.println("Key is " + key + " Value is " + con.getHeaderField(i));
if( key.equals(FISConstants.SET_COOKIE) ){
value = con.getHeaderField( i );
return value;
}
}
return null;
}
}


SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
sslContext.init( null, xtmArray, new java.security.SecureRandom() );
} catch( GeneralSecurityException gse ) {
// Print out some error message and deal with this exception…
}
// Set the default SocketFactory and HostnameVerifier
// for javax.net.ssl.HttpsURLConnection
if( sslContext != null ) {
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory() );
}
HttpsURLConnection.setDefaultHostnameVerifier( hnv );



private X509TrustManager xtm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain,
String authType) {}
public void checkServerTrusted(X509Certificate[] chain,
String authType) {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};

//Added by Sambasiva for implementation of Https
private HostnameVerifier hnv = new HostnameVerifier() {
public boolean verify(String hostname,SSLSession session) {
System.out.println("hostname: " + hostname);
return true;
}
};


public void genBatchRun(String args[]) {
}
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please http://faq.javaranch.com/java/UseCodeTags. You can edit your post to add them.

And please, don't use blue as your text color. Black is much easier to read.
 
sambasiva kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please provide me an approach to resolve this issue.
Thanks in advance.
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still can't read your code properly. Where are those code tags?
 
sambasiva kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I tried by including the following to establish the cookie


<sun-web-app>
<session-config>
<session-manager persistence-type="memory">
<manager-properties>
<property name="reapIntervalSeconds" value="20" />
</manager-properties>
</session-manager>
<session-properties>
<property name="enableCookies" value="true" />
</session-properties>
<cookie-properties>
<property name="cookiePath" value="/" />
<property name="cookieMaxAgeSeconds" value="-1" />
</cookie-properties>
</session-config>
<jsp-config/>
</sun-web-app>

in the sun-web.xml.


Please find the code that I am using.
-------------------------------------------------------------------------------------------------------------------------------


 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you really not indenting your code?
 
sambasiva kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
sambasiva kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody please help me to resolve this issue.
Thanks in advance.
 
sambasiva kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please find below the code that I am using to establish the cookie.
And you can see in the log the cookie is not getting established.
-------------------------------------------------------------------------------------------------------------------------
The value of cookie in the process request is ............... null
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2155)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2621)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:734)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:251)
at com.orbitreasury.fixedincome.presentation.util.AppServCommunication.receiveRequest(AppServCommunication.java:227)
at com.orbitreasury.fixedincome.presentation.util.AppServCommunication.processRequest(AppServCommunication.java:166)
at com.orbitreasury.fixedincome.control.ServerResets.main(ServerResets.java:62)
-------------------------------------------------------------------------------------------------------------------------

Please advice me on how to establish the cookie?
Thanks in advance.
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception is thrown from the ObjectInputStream's constructor, which indicates that the required meta-data bytes are missing. Apparently the other side of the connection does not use an ObjectOutputStream for writing when you receive this exception. Are you sure that this connection is coming from your application, and not something else?
 
sambasiva kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please find below the code that I am using to write as well as read the InputStream.

 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But are you sure that it is this piece of code that connects?
 
sambasiva kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find below the code where I am opening the connection and setting the cookie.

 
sambasiva kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please advice me in the below request.
Thanks in advance.
 
sambasiva kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody please advice me.
Thanks in advance.
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hate to say it, but it seems that the simple fact is that nobody knows why it isn't working.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic