URL url;
try
{
url = new URL(
"http://www.baidu.com");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.connect();
BufferedReader dis = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream(),"utf-8"));
String readLine = null;
String type = urlConnection.getContentType();
System.out.println(type);
long time = urlConnection.getDate();
String content = urlConnection.getContentEncoding();
System.out.println("content:"+content);
Date date = new Date(time);
System.out.println(time +":"+date);
while (true)
{
String temp = dis.readLine();
if (temp == null)
{
break;
}
//readLine = new String(temp.getBytes(), "utf-8");
System.out.println(temp);
} dis.close();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}标红的那一句 我发现加不加都没什么关系 好像没有用一样。我想问下,urlConnection.connect()这句话到底有什么用,url.openConnection()这句话又有什么用,他们的区别是什么?urlConnection.connect()到底什么时候用?
解决方案 »
- JFrame不能用add()?还是孙卫琴姐姐说错了?
- 我的MyEclipse中为何不能识别javax.swing.GroupLayout!!!
- 怎样显示滚动条,帮我看看我的代码那里出现问题?
- 诚信请教,先谢过:DocumentListener中的问题
- 问题 求解啊 急!!
- java新手问题
- 求core java 中文译本
- classpath和package问题:
- 大家好!!高分请教各位,Applet的CGI通信能否打开新网页
- 没人理我呀,5555555~~~~~~~~~~~~~~~~
- RandomAccessFile.seek与skipBytes
- 如何在FlowLayoutPanel加滚动条,右边的和下边的
URL url;
try
{
url = new URL(
"http://www.baidu.com");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setDoInput(true);(1)
System.out.println("responseCode1:"+urlConnection.getResponseCode());
urlConnection.connect();
urlConnection.setDoInput(true);(2)
System.out.println("responseCode2:"+urlConnection.getResponseCode());
BufferedReader dis = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream(),"utf-8"));
System.out.println("responseCode3:"+urlConnection.getResponseCode());
String readLine = null;
String type = urlConnection.getContentType();
System.out.println(type);
long time = urlConnection.getDate();
String content = urlConnection.getContentEncoding();
System.out.println("content:"+content);
Date date = new Date(time);
System.out.println(time +":"+date);
while (true)
{
String temp = dis.readLine();
if (temp == null)
{
break;
}
//readLine = new String(temp.getBytes(), "utf-8");
//System.out.println(temp);
} dis.close();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}iambic 说的我今天又验证了下
URL url;
try
{
url = new URL(
"http://www.baidu.com");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setDoInput(true);
System.out.println("responseCode1:"+urlConnection.getResponseCode());
urlConnection.setConnectTimeout(5000);
urlConnection.connect();
urlConnection.setDoInput(true);
System.out.println("responseCode2:"+urlConnection.getResponseCode());
BufferedReader dis = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream(),"utf-8"));
System.out.println("responseCode3:"+urlConnection.getResponseCode());
String readLine = null;
String type = urlConnection.getContentType();
System.out.println(type);
long time = urlConnection.getDate();
String content = urlConnection.getContentEncoding();
System.out.println("content:"+content);
Date date = new Date(time);
System.out.println(time +":"+date);
while (true)
{
String temp = dis.readLine();
if (temp == null)
{
break;
}
//readLine = new String(temp.getBytes(), "utf-8");
//System.out.println(temp);
} dis.close();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}iambic 说的我今天又验证了下 发现在第一个位置和第二个未知同时调用 urlConnection.setDoInput(true); 发现只有第二个位置才会抛出异常 所以他openConnection之后不是connected的。不知道谁能帮忙在解释下 调用和不调用connect()到底有没有区别。