can you send me some example code? Now the code i'm trying like this
URL urlServlet = new URL("https://111.111.111/TestServlet"); con = (HttpsURLConnection) urlServlet.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setDefaultUseCaches(false); con.setAllowUserInteraction(true); con.setRequestMethod("GET"); con.setRequestProperty("Content-Length", "0"); con.setRequestProperty("Content-Type", "text/html;charset=UTF-8"); ObjectOutputStream outstream = new ObjectOutputStream(con.getOutputStream()); outstream.writeObject("hi"); outstream.flush(); outstream.close(); But the above code does not reach the requred servlet in the specified URL. Is there any problem in the code. Please help me to solve this problem
But the above code does not reach the requred servlet in the specified URL. Is there any problem in the code. Please help me to solve this problem
Exactly what happens? You do not appear to be reading the response so you are missing possible error codes. Also I dont see any provision for exception handling, there are many places in your code that could be throwing an exception which would be diagnostic.
Joined: Jun 10, 2008
thanks for the reply.I found the error. Now i want to make connection to servlet using URL class. The above servlet is in j_security_check enabled application. if i connect using in normal way through URL class,i am getting IOException 405 servlet response code. how to make connection using URL?
Author and all-around good cowpoke
Joined: Mar 22, 2000
Error 405 is the standard response code for "method not allowed" - you can look these up in the JavaDocs for HttpURLConnection class.
The method your code specifies is "GET" - apparently the responding servlet does not like a GET - which makes sense because you are trying to send data in the body of the request. Normally done with a POST or PUT.
Why are you setting Content-Length to zero? Why are you trying to send a Java object instead of text like your Content-Type specifies?
U may use POST method too. It's like uploading files to server. Set enctype="multipart/form-data" and parse it by org.apache.commons.fileupload.disk.DiskFileItemFactory, org.apache.commons.fileupload.servlet.ServletFileUpload (google for it).
Computers are like air conditioners - They stop working properly when you open Windows