PrintWriter out = new PrintWriter(connection.getOutputStream()); //DataOutputStream out = new DataOutputStream(connection.getOutputStream()); //只有使用PrintWriter才成功,否则会失败!
// encode the message String name = "name="+URLEncoder.encode("Qusay Mahmoud");//, "UTF-8" String email = "email="+URLEncoder.encode("[email protected]");//, "UTF-8" // send the encoded message out.println(name+"&"+email); //out.writeChars(name+"&"+email); out.close(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); System.in.read (); } }/** output * * <html><head><title>Names</title></head><body><h2>Info Submitted</h2> * <p>Name: Qusay Mahmoud<p>Email: [email protected]</body></html> *///代理服务器和防火墙
while (int i = 0; i < bbb.length; i++)
System.out.print(Integer.toHexString(bbb[i]) & 0xFF)
System.out.println();
/**
* 示例代码 5 中是这个例子的实现 (使用 HttpURLConnection 类),
* 它展示了如何使用 POST 方法向服务器发送数据。你会看到:
*
* 1.为 CGI 脚本打开连接和 I/O 流
* 2.设置请求方法为 POST
* 3.使用 URLEncoder.encode 方法对消息进行编码 (URLDecoder.decode 方法可以用于解码)
* 4.向 CGI 脚本发送已经编码的消息
* 5.接收服务器返回的消息并在控制台打印出来
*/
import java.io.*;
import java.net.*;public class PostExample {
public static void main(String[] argv) throws Exception {
URL url = new URL("http://www.javacourses.com/cgi-bin/names.cgi");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
//DataOutputStream out = new DataOutputStream(connection.getOutputStream());
//只有使用PrintWriter才成功,否则会失败!
// encode the message
String name = "name="+URLEncoder.encode("Qusay Mahmoud");//, "UTF-8"
String email = "email="+URLEncoder.encode("[email protected]");//, "UTF-8"
// send the encoded message
out.println(name+"&"+email);
//out.writeChars(name+"&"+email);
out.close();
BufferedReader in
= new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
System.in.read ();
}
}/** output
*
* <html><head><title>Names</title></head><body><h2>Info Submitted</h2>
* <p>Name: Qusay Mahmoud<p>Email: [email protected]</body></html>
*///代理服务器和防火墙