Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: how can I use POST in applet and open the result in a browser window?
|
Posted: Oct 3, 2002 7:26 PM
|
|
try{ String urlPrefix = "http://127.0.0.1:8080/Servlets/YourServlet"; String query = URLEncoder.encode("name", "8859_1") + "=" + URLEncoder.encode("John Smith", "8859_1"); URL url = new URL(urlPrefix); URLConnection connection = url.openConnection(); connection.setDoOutput(true); OutputStream os = connection.getOutputStream(); OutputStream bos = new BufferedOutputStream(os); OutputStreamWriter osw = new OutputStreamWriter(bos, "8859_1"); osw.write(query + "\r\n"); osw.flush(); osw.close(); String response = ""; InputStreamReader isr = new InputStreamReader(new BufferedInputStream(connection.getInputStream())); int c = 0; while ((c = isr.read()) != -1){ response = response + String.valueOf((char)c); } JOptionPane.showMessageDialog(null , response);
}catch(MalformedURLException murle){ System.err.println("MalformedURLException: " + murle.getMessage()); }catch(IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); }
|
|