how do transfer data from one server to another server
elay Raja
Greenhorn
Joined: Jun 10, 2008
Posts: 27
posted
0
i want to send xml from one server to another server(ie.cross domain).How can i send it through servlet.is it possible to send it using URL class by making connection to required servlet.
For example, if there are two WebApplication1 & WebApplication2. I want to send the data from webApplication1 to webapplication2 through servlet.
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
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12267
1
posted
0
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.
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?
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12267
1
posted
0
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?
You must use PUT method. Perhaps you can see this link http://kickjava.com/996.htm There is a brief example of using PUT method for HttpURLConnection. [ August 20, 2008: Message edited by: Hendy Setyo Mulyo ]
Hendy Setyo Mulyo
SCJP 1.4 (95%), SCWCD 1.4 (94%)
Evgeniy Bulanov
Greenhorn
Joined: Dec 19, 2005
Posts: 23
posted
0
Hi elay Raja,
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).
JK
Computers are like air conditioners - They stop working properly when you open Windows