URL和HttpURLConnection的使用
java.lang.Object
|--java.net.URL
java.lang.Object
|--java.net.URLConnection
|--java.net.HttpURLConnection
1、模拟手机端
1 importjava.io.OutputStream;2 importjava.io.PrintWriter;3 importjava.net.HttpURLConnection;4 importjava.net.URL;5 importjava.net.URLEncoder;6
7 //模拟手机客户端
8 public classPhone {9 public static void main(String[] args) throwsException {10 //建立与服务端的连接
11 URL url = new URL("http://localhost:8080/day26/Server");12 //取得与服务端的连接
13 HttpURLConnection conn =(HttpURLConnection)url.openConnection();14 conn.setDoOutput(true);//防止报Exception in thread "main" java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)15 //向服务端发送请求
16 OutputStream os =conn.getOutputStream();17 PrintWriter pw = newPrintWriter(os);18 String username = "杰克"