1. 程式人生 > >HTTPURLConneciton的post提交方式

HTTPURLConneciton的post提交方式

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());