Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SSL Connection ....doesnt send response back in jdk1.4!!!

 
lusha tak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am including my test code which establishes a ssl connection to a server and posts an HTTP Post request .which then sends the response back.
This code works fine with jdk1.3.1_01.
But when i try to run the same code sample on j2sdk1.4.2_02, i get Socket Exception. Unexcpected end of file error just after the System.out.println(hello....10)statement. On searching the net i found there is a bug in URLConnection's getContent Method of j2sdk1.4.2_02....which has been fixed in j2sdk1.4.2_03....so i downladed the same.....and tried to run the same....but now it gives java.io.IOException....
what could be the reason??
pl. throw some light..
Thanks and Regards
Lusha

code:
--------------------------------------------------------------------------------
import javax.servlet.*;import javax.servlet.http.*;import java.util.*;import java.io.*;import java.net.*;import java.security.Security;public class BTServlet extends HttpServlet {public void init(){// Setting proxy server hostSystem.setProperty("https.proxyHost", "3.123.456.209");// Setting proxy server portSystem.setProperty("https.proxyPort", "80");Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");// Setting security certicicate locationSystem.setProperty("javax.net.ssl.trustStore","C:\\j2sdk1.4.2_03\\jre\\lib\\security\\cacerts");//System.setProperty("javax.net.debug","ssl,handshake,data,trustmanager");System.setProperty("security.SSL.ignoreHostnameVerification", "true" );}public void service(HttpServletRequest req,HttpServletResponse res){int c;URL url = null;Reader reader = null;Writer writer = null;StringBuffer sb = new StringBuffer("");try{String reqType = req.getParameter("reqType");if(reqType == null){reqType = "ping.xml";}String prefix = getServletContext().getRealPath("/");System.out.println("#### path="+prefix);url = new URL("https://services.xyz.com/abc");reader = new FileReader(prefix+"\\WEB-INF\\req\\"+reqType);// set an Authenticator to generate username/passwordAuthenticator auth = new Authenticator() {protected PasswordAuthentication getPasswordAuthentication(){String username = "qgdw8gi5zPVN";String password = "vazpCrQjkBVM";return new PasswordAuthentication( username,password.toCharArray() );}};Authenticator.setDefault( auth );java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();connection.setRequestMethod( "POST" );connection.setDoOutput( true );connection.setRequestProperty( "Content-Type", "text/xml" );// connect and open the writer for POST bodyconnection.connect();java.io.Writer connWriter = new java.io.OutputStreamWriter( connection.getOutputStream() );// write the post bodywhile ( (c = reader.read()) != -1 )connWriter.write( c );connWriter.flush();// close the POST bodySystem.out.printlnconnection.getOutputStream().close();System.out.println("hello....10");

String str = connection.getContentType();
System.out.println("hello....10.5"+str);
// get the response body reader
java.lang.Object response = connection.getContent();
java.io.Reader connReader = new java.io.InputStreamReader( (java.io.InputStream) response );
// read the response to the output
while ( (c = connReader.read()) != -1 )
sb.append((char)c);
req.setAttribute("RESP_XML", sb.toString());
}
catch (Exception e)
{
System.out.println("Error:"+e.toString());
req.setAttribute("RESP_XML", e.toString());
}
finally{
try{
if(reader != null)
reader.close();
if(writer != null)
writer.close();
res.setContentType("text/html");
RequestDispatcher dispatcher;
dispatcher = getServletContext().getRequestDispatcher("/abcd.jsp");
dispatcher.forward(req, res);
}
catch (Exception e1) {
System.out.println("MSG:"+e1.toString());
}
}
}
}
-----------------------------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic