-
//POST请求不能使用缓存
-
urlConn.setUseCaches(false);
-
//关闭连接
-
urlConn.disConnection();
HttpURLConnection默认使用GET方式,例如下面代码所示:
- //使用HttpURLConnection打开连接
2.HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
- //得到读取的内容(流)
4.InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
-
// 为输出创建BufferedReader
-
BufferedReader buffer = new BufferedReader(in); 7. String inputLine = null;
-
//使用循环来读取获得的数据
-
while (((inputLine = buffer.readLine()) != null))
-
{
-
//我们在每一行后面加上一个"\n"来换行
-
resultData += inputLine + “\n”;
-
}
-
//关闭InputStreamReader
-
in.close();
-
//关闭http连接
-
urlConn.disconnect();
如果需要使用POST方式,则需要setRequestMethod设置。代码如下:
1.String httpUrl = “http://192.168.1.110:8080/httpget.jsp”;
-
//获得的数据
-
String resultData = “”;
-
URL url = null;
-
try
-
{