forked from patniemeyer/learningjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPost.java
More file actions
87 lines (75 loc) · 2.67 KB
/
Copy pathPost.java
File metadata and controls
87 lines (75 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//file: Post.java
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Post 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 Post( String postURL ) {
this.postURL = postURL;
setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 5));
JButton postButton = new JButton("Post");
postButton.addActionListener( this );
setLayout( new GridBagLayout( ) );
constraints.fill = GridBagConstraints.HORIZONTAL;
addGB( new JLabel("Name ", JLabel.TRAILING), 0, 0 );
addGB( nameField = new JTextField(20), 1, 0 );
addGB( new JLabel("Password ", JLabel.TRAILING), 0, 1 );
addGB( passwordField = new JPasswordField(20), 1, 1 );
constraints.fill = GridBagConstraints.NONE;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.EAST;
addGB( postButton, 1, 2 );
}
public void actionPerformed(ActionEvent e) {
postData( );
}
protected 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( 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...");
return;
}
//InputStream in = urlcon.getInputStream( );
// ...
} catch (MalformedURLException e) {
System.out.println(e); // bad postURL
} catch (IOException e2) {
System.out.println(e2); // I/O error
}
}
public static void main( String [] args ) {
JFrame frame = new JFrame("SimplePost");
frame.add( new Post( args[0] ), "Center" );
frame.pack( );
frame.setVisible(true);
}
}