在程序的最前面你就已经定义了private InputStream in = null;
在main方法里面你又定义了InputStream in = null;
不知道是不是这个原因

解决方案 »

  1.   

    改:
        for (int i=0; i<httpHeader.length; i++) 
        { 
          dos.writeBytes(httpHeader[i]); 
          dos.writeByte(13); 
          dos.writeByte(10); 
        } 
        dos.writeByte(13); 
        dos.writeByte(10); 
      }

    为:
        for (int i=0; i<httpHeader.length; i++) 
        { 
          dos.writeBytes(httpHeader[i]); 
          dos.writeByte(13); 
          dos.writeByte(10); 
        } 
        dos.writeByte(13); 
        dos.writeByte(10); 
      }多了一个括号!如果还有NullPointerException问题的话,那么你的classpath有问题,请检查你的classpath设置。
      

  2.   

    2 bobosji(波波司机):
    那个括号应该不存在,我发帖子时改了些写错了,
    程序是没有的,错误依然存在
    2 vdragon(紫龙:
    那个定义只是个指向,没有问题的
      

  3.   

    你的程序我运行没有问题,检查你的classpath设置
      

  4.   

    我要是在connect方法里面加入try就出现:
    java.net.UnknownHostException: www.csdn.net
            at java.net.InetAddress.getAllByName0(InetAddress.java:571)
            at java.net.InetAddress.getAllByName0(InetAddress.java:540)
            at java.net.InetAddress.getByName(InetAddress.java:449)
            at java.net.Socket.<init>(Socket.java:100)
            at MyURLConnection.connect(MyURLConnection.java:31)
            at MyURLConnection.main(MyURLConnection.java:64)
    不会是classpath问题!
      

  5.   

    你那 connect() 里面的 url 哪来的?
    稍微修改了一下(加上了 url 那个成员变量),在我的机器上运行一切正常啊?而且取 web 页面也不是你这样取的:
    先要得到 Content-length 头信息,然后根据这个整数值取相应字节数的数据。
      

  6.   

    呵呵,为什么会有这样的情况??程序运行得很好啊,网页也抓下来了!不知道你那里是什么情况了,该不会是Applet改过来的吧??试试你自己贴出来的程序,很好的,如果有问题,你可以参照解决。
      

  7.   

    我试过了,不改动也没有问题!可能是你的classpath设置有我问题
      

  8.   

    各位大哥的jdk的版本是??
    我使用jb5的1.3版
      

  9.   

    import java.net.*; 
    import java.io.*; public class MyURLConnection

    // timeout is measured in milliseconds, default is infinite 
      private int timeout = 0;  
      private InputStream in = null; 
      private OutputStream out = null; 
      private Socket soc = null; 
      private String url=null;
      
      // HTTP only 
      private String [] httpHeader = 
      { 
        "GET / HTTP/1.1", 
        "User-Agent: Java1.3.0", 
        "Host: ", 
        "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", 
        "Connection: keep-alive" 
      }; 
      
      public MyURLConnection(String url, int timeout) 
      {     this.timeout = timeout; 
        this.url=url;
        httpHeader[2] += url; // HTTP only 
      } 
      
      public void connect() throws IOException 
      { 
        soc = new Socket(url, 80); 
        soc.setSoTimeout(timeout); 
        in = soc.getInputStream(); 
        out = soc.getOutputStream(); 
        DataOutputStream dos = new DataOutputStream(out); 
        
        // Http only 
        for (int i=0; i<httpHeader.length; i++) 
        { 
          dos.writeBytes(httpHeader[i]); 
          dos.writeByte(13); 
          dos.writeByte(10); 
        } 
        dos.writeByte(13); 
        dos.writeByte(10); 

        
      public void close() throws IOException 
      { 
        soc.close(); 
      } 
      
      public InputStream getInputStream() { return in; } 
      
      public OutputStream getOutputStream() { return out; } 
      
      public static void main(String [] args) 
      { 
        InputStream in = null; 
        MyURLConnection conn = null; 
        try 
        { 
          conn = new MyURLConnection("http://www.csdn.net", 10000); 
          conn.connect(); 
          in = conn.getInputStream(); 
          BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
          String linein; 
          while ((linein = br.readLine()) != null) 
          System.out.println(linein); 
        } 
        catch (Exception e){e.printStackTrace();} 
        finally 
        { 
        try 
        { 
          if (in != null) in.close(); 
          if (conn != null) conn.close(); 
        } 
        catch (Exception e){} 
        } 
        } 

      

  10.   

    看看这个吧,功能很全,有原代码,可以参考。
    http://www.innovation.ch/java/HTTPClient/
      

  11.   

    gdsean你小子居然用我的ID!!!
    快给分赔偿精神损失!
      

  12.   

    2 skyyoung:
    那些代码我看了一下,太多内容不容易明白,我希望的是连接可以
    用时间来限制,有的网站会让程序连接很长时间,有办法解决吗??