File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Non-browser Java app - posting to a URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Non-browser Java app - posting to a URL" Watch "Non-browser Java app - posting to a URL" New topic

Non-browser Java app - posting to a URL

Rox Rox

Joined: Aug 14, 2003
Posts: 1
Obj: Program for Posting a XML file to a listener URL and get back the XML response after processing.
Constraint: That listener is protected by a thirdparty SSO tool and so a credential cookie needs to be built. this is simple and was acheived, like the the program first talks to the SSO server, establishes a session and obtains a cookie whic can be inserted to the ensuing posts to the URL.
Problem: Unable to post the file ..for details pls look at the code that i am trying ..ALL SUGGESTIONS welcome!!
public void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
SMJavaAgent smja = new SMJavaAgent();
smtoken = smja.retSSOToken();
Cookie cSMSession = new Cookie("SMSESSION", smtoken);


URL u = new URL(urlString);
//IS there a way of achieving the below functionality using
//servlet's forms posting to a non-browser URL browsers are involved here
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setRequestProperty("Cookie", cSMSession.getName()+"="+cSMSession.getValue());
OutputStream out = conn.getOutputStream();

//posting hte fie to the output stream
BufferedReader in = new BufferedReader(new FileReader(file));
while((s = in.readLine())!= null)
xml_line = xml_line+s;

writer.println("The XML file is ..\n");

//getting back the response from the URL
InputStream ins = conn.getInputStream();

int i = ins.available();
if ( i <= 0) break;
byte b[] = new byte[i];;
String tmp = new String(b);

//close all the open resources and disconnect
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
That looks pretty reasonable, but I think you need to flush that output stream. Probably the other end is waiting for the last bit of XML that is stuck in the buffer.
I agree. Here's the link:
subject: Non-browser Java app - posting to a URL
It's not a secret anymore!