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()到底什么时候用?

解决方案 »

  1.   

    发现不知道为什么没标红? 我说的好像没用的那一句是urlConnection.connect()
      

  2.   

    openConnection返回的connection应该已经是connected的了。如果disconnect之后还想重新打开应该可以再手动connect。
      

  3.   

    谢谢iambic  openConnection会帮你调用connect()是吧?这个JDK看不到源码 比较郁闷
      

  4.   


    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()到底有没有区别。