經過Android重審GET和POST請求

學習Java web的時候老是背GET和POST的區別,根本不知道GET和POST有什麼區別!java

百度一下它們的區別就有答案!可是不能理解!web

POST的代碼:服務器

public void run() {
                String path = "http://10.31.2.6:8080/06_Server/servlet/login";
                try {
                    URL url = new URL(path);
                    
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    
                    conn.setRequestMethod("POST");
                    conn.setReadTimeout(5000);
                    conn.setConnectTimeout(5000);
                    
                    /**
                     * 設置POST請求特殊的東西
                     * name="+ URLEncoder.encode(name) +"&pass=" + pass
                     */
                    //拼接出要提交的數據的字符串
                    String data = "name=" + URLEncoder.encode(name) + "&pass=" + pass;
                    //添加post請求的兩行屬性
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    conn.setRequestProperty("Content-Length", data.length() + "");
                    
                    //將協議體提交給服務器
                    //打開輸出流
                    conn.setDoOutput(true);
                    //拿到輸出流
                    OutputStream os = conn.getOutputStream();
                    //放到輸出流中,提交到服務器
                    os.write(data.getBytes());
                    
                    conn.connect();
                    if(conn.getResponseCode() == 200) {
                        InputStream in = conn.getInputStream();
                        //將輸入流變成字符串
                        String result = Utils.fromStream2String(in);    
                        
                        Message msg = handler.obtainMessage();
                        msg.obj = result;
                        handler.sendMessage(msg);                        
                        
                    } else {
                        System.out.println("系統出錯");
                    }
                    
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }

GET的代碼:app

public void run() {
                String path = "http://10.31.2.6:8080/06_Server/servlet/login?name="+ URLEncoder.encode(name) +"&pass=" + pass;
                try {
                    URL url = new URL(path);
                    
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    
                    conn.setRequestMethod("GET");
                    conn.setReadTimeout(5000);
                    conn.setConnectTimeout(5000);
                    
                    conn.connect();
                    
                    if(conn.getResponseCode() == 200) {
                        InputStream in = conn.getInputStream();
                        //將輸入流變成字符串
                        String result = Utils.fromStream2String(in);    
                        
                        Message msg = handler.obtainMessage();
                        msg.obj = result;
                        handler.sendMessage(msg);                        
                        
                    } else {
                        System.out.println("系統出錯");
                    }
                    
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }

代碼中就體現了GET和POST請求的區別!
post

相關文章
相關標籤/搜索