aspose file tools*
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
Author

Non-browser Java app - posting to a URL

Rox Rox
Greenhorn

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);

response.addCookie(cSMSession);

URL u = new URL(urlString);
//IS there a way of achieving the below functionality using
//servlet's forms posting to a non-browser URL ..no browsers are involved here
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
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");
writer.println(xml_line);
writer.println("\n\n\n");
out.write(xml_line.getBytes());

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



while(true)
{
int i = ins.available();
if ( i <= 0) break;
byte b[] = new byte[i];

ins.read(b);
String tmp = new String(b);
writer.println(tmp);
}

//close all the open resources and disconnect
in.close();
ins.close();
out.close();
conn.disconnect();
}
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
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.
Bill
 
 
subject: Non-browser Java app - posting to a URL