HTTPURLConneciton的post提交方式
阿新 • • 發佈:2018-12-17
URL url=new URL(path);
//得到httpurlconnection物件 HttpURLConnection httpURLConnection= (HttpsURLConnection) url.openConnection();
//設定請求方式post httpURLConnection.setRequestMethod(“POST”);
//設定連線時間 httpURLConnection.setConnectTimeout(5000);
//設定http請求資料型別 httpURLConnection.setRequestProperty(“Content-Type”,“application/x-www-form-urlencoded”);
//資料 String data=“mobile=”+name1+"&password="+pwd1;
//告訴伺服器我給你提交的資料有多大 httpURLConnection.setRequestProperty(“Content-Length”,data.length()+"");
//要記得指定給伺服器寫入資料的開關開啟 httpURLConnection.setDoInput(true);
//開始往伺服器傳入資料 httpURLConnection.getOutputStream().write(data.getBytes());