HttpURLConnection

使用HttpURLConnection的步骤

URL url = new URL(http://www.baidu.com);//创建一个URL对象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();//调用URL对象的openConnection( )来获取HttpURLConnection对象实例
connection.setRequestMethod("GET");//设置请求方式,GET或POST
connection.setConnectTimeout(1000);//设置连接超时毫秒数
connection.setReadTimeout(1000);//读取超时毫秒数
InputStream in=connection.getInputStream();//获取输入流
connection.disconnect();//关闭连接

如果要提交数据给服务器:

connection.setRequestMethod("POST");
DataOutputStream ops=new DataOutputSteam(connection.getOutputStream());
out.writeBytes("username=neme&password=123");

你可能感兴趣的:(HttpURLConnection)