在程序的最前面你就已经定义了private InputStream in = null;
在main方法里面你又定义了InputStream in = null;
不知道是不是这个原因
在main方法里面你又定义了InputStream in = null;
不知道是不是这个原因
解决方案 »
- 不允许的操作: operation cannot be mixed with Oracle-style batching
- 急救:JSP中调用(J2SE写的坦克大战,有7个类,一个主函数)!!
- 关于JAVA中数组长度为0的情况
- 客户端:多线程使用单个SOCKET发数
- 关于JAVA输入输出流疑问
- 到底 Java 是如何传递参数的?是by value或by reference?
- 该怎么学java
- ???关于applet
- 请问Java与asp的优缺点?我是初学Web者,选取哪种好?
- 用java.awt.Graphics画线,能否设定线的宽幅?
- 问道题吧
- 请问如何实现鼠标拖放选购的物品到购物篮的程序!如果嫌分不够再加!!!
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 vdragon(紫龙:
那个定义只是个指向,没有问题的
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问题!
稍微修改了一下(加上了 url 那个成员变量),在我的机器上运行一切正常啊?而且取 web 页面也不是你这样取的:
先要得到 Content-length 头信息,然后根据这个整数值取相应字节数的数据。
我使用jb5的1.3版
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){}
}
}
}
http://www.innovation.ch/java/HTTPClient/
快给分赔偿精神损失!
那些代码我看了一下,太多内容不容易明白,我希望的是连接可以
用时间来限制,有的网站会让程序连接很长时间,有办法解决吗??