1、先从URL拿到HttpURLConnection:
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
2、设置参数:
urlConnection.setConnectTimeout(3000); urlConnection.setRequestMethod("POST"); urlConnection.setDoInput(true);// 表示从服务器获取数据 urlConnection.setDoOutput(true);// 表示向服务器写数据
3、获得OutputStream,状态码,写数据等:
// 获得输出流,向服务器输出数据 OutputStream outputStream = urlConnection.getOutputStream(); outputStream.write(mydata,0,mydata.length); outputStream.close(); // 获得服务器响应的结果和状态码 int responseCode = urlConnection.getResponseCode();