导航

Http编程 ___ 1

Posted on 2013-08-04 16:52  ChanHuman  阅读(240)  评论(0编辑  收藏  举报

 

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