Outra coisa… alguem me ajude… eu consegui este código, mas ele só da a mensagem “Bad post…”
[code]import java.net.;
import java.io.;
import java.awt.;
import java.awt.event.;
import javax.swing.*;
public class LerURL extends JPanel implements ActionListener {
JTextField nameField, passwordField;
String postURL;
GridBagConstraints constraints = new GridBagConstraints( );
void addGB( Component component, int x, int y ) {
constraints.gridx = x; constraints.gridy = y;
add ( component, constraints );
}
public LerURL() {
this.postURL = "http://www.meusite/login.php";
JButton postButton = new JButton("Post");
postButton.addActionListener( this );
setLayout( new GridBagLayout( ) );
addGB( new JLabel("Login:"), 0,0 );
addGB( nameField = new JTextField(20), 1,0 );
addGB( new JLabel("Password:"), 0,1 );
addGB( passwordField = new JPasswordField(20),1,1 );
constraints.gridwidth = 2;
addGB( postButton, 0,2 );
}
public void actionPerformed(ActionEvent e) {
postData();
}
protected void postData( ) {
try {
StringBuffer sb = new StringBuffer( );
sb.append( URLEncoder.encode("login", "UTF-8") +"=" );
sb.append( URLEncoder.encode("teste", "UTF-8") );
//sb.append( URLEncoder.encode(nameField.getText(), "UTF-8") );
sb.append( "&" + URLEncoder.encode("password", "UTF-8")+"=" );
sb.append( URLEncoder.encode("teste", "UTF-8") );
//sb.append( URLEncoder.encode(passwordField.getText(), "UTF-8") );
String formData = sb.toString( );
URL url = new URL( 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("Posted ok!");
else {
System.out.println("Bad post...");
}
//InputStream in = urlcon.getInputStream( );
// ...
// Get the response
// BufferedReader rd = new BufferedReader(new InputStreamReader(urlcon.getInputStream()));
// String line;
// while ((line = rd.readLine()) != null) {
// System.out.println(line);
// }
} catch (MalformedURLException e) {
System.out.println(e); // bad postURL
} catch (IOException e2) {
System.out.println(e2); // I/O error
}
System.exit(0);
}
public static void main( String [] args ) {
JFrame frame = new JFrame("SimplePost");
frame.getContentPane().add( new LerURL(), "Center" );
frame.pack();
frame.setVisible(true);
}
}[/code]