Postpublic class Post extends Applet implements ActionListener
Fields Summary |
---|
TextField | nameField | TextField | passwordField | GridBagConstraints | constraints |
Methods Summary |
---|
public void | actionPerformed(java.awt.event.ActionEvent e)
postData();
| void | addGB(java.awt.Component component, int x, int y)
constraints.gridx = x; constraints.gridy = y;
add ( component, constraints );
| public void | init()
Button postButton = new Button("Post");
postButton.addActionListener( this );
setLayout( new GridBagLayout() );
addGB( new Label("Name:"), 0,0 );
addGB( nameField = new TextField(20), 1,0 );
addGB( new Label("Password:"), 0,1 );
addGB( passwordField = new TextField(20),1,1 );
Panel p = new Panel();
p.add ( postButton );
constraints.gridwidth = 2;
addGB( p, 0,2 );
| private void | postData()
StringBuffer sb = new StringBuffer();
sb.append( URLEncoder.encode("Name") + "=" );
sb.append( URLEncoder.encode(nameField.getText()) );
sb.append( "&" + URLEncoder.encode("Password") + "=" );
sb.append( URLEncoder.encode(passwordField.getText()) );
String formData = sb.toString();
try {
URL url = new URL( getParameter("postURL") );
HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
urlcon.setRequestMethod("POST");
urlcon.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
urlcon.setDoOutput(true);
urlcon.setDoInput(true);
PrintWriter pout = new PrintWriter( new OutputStreamWriter(
urlcon.getOutputStream(), "8859_1"), true );
pout.print( formData );
pout.flush();
// read results...
if ( urlcon.getResponseCode() != HttpURLConnection.HTTP_OK ) {
System.out.println("Bad post...");
return;
}
InputStream in = urlcon.getInputStream();
// ...
} catch (MalformedURLException e) {
System.out.println(e); // bad postURL
} catch (IOException e2) {
System.out.println(e2); // I/O error
}
|
|