HttpURLConnection

2017-07-23  本文已影响0人  NullUser

使用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");
上一篇下一篇

猜你喜欢

热点阅读