当前位置: 技术问答>java相关
困扰我多时的问题,大家来帮忙
来源: 互联网 发布时间:2015-04-01
本文导语: 我想用socket将一个网页的内容拉下来,但是不成功,请大家帮忙提供代码。 这是我的代码: public class TestSocket { public static void main(java.lang.String[] args) { try { InetAddr...
我想用socket将一个网页的内容拉下来,但是不成功,请大家帮忙提供代码。
这是我的代码:
public class TestSocket {
public static void main(java.lang.String[] args) {
try {
InetAddress addr = InetAddress.getByName("www.cnmaya.com");
Socket s = new Socket(addr,80);
//Socket s = new Socket("202.109.110.41",80);
DataInputStream sin =new DataInputStream(s.getInputStream());
DataOutputStream sout =new DataOutputStream(s.getOutputStream());
while (sin.available()!=0) {
System.out.print(sin.readByte());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我的代码:
public class TestSocket {
public static void main(java.lang.String[] args) {
try {
InetAddress addr = InetAddress.getByName("www.cnmaya.com");
Socket s = new Socket(addr,80);
//Socket s = new Socket("202.109.110.41",80);
DataInputStream sin =new DataInputStream(s.getInputStream());
DataOutputStream sout =new DataOutputStream(s.getOutputStream());
while (sin.available()!=0) {
System.out.print(sin.readByte());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
sin.available()==0
所以没有得到输入流
你要是下载网页,使用URL类得了
URL myurl = new URL("/tech-qa-java/adf.com");
InputStream ins = myurl.openStream();
...
所以没有得到输入流
你要是下载网页,使用URL类得了
URL myurl = new URL("/tech-qa-java/adf.com");
InputStream ins = myurl.openStream();
...
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。