import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;public class HtmlSourceView {
public static void main(String[] args) {
InputStream in = null;
OutputStream out = null;
try {
URL url = new URL("http://www.csdn.net");
in = url.openStream();
if (args.length == 2) {
out = new FileOutputStream(args[1]);
} else
out = System.out;
byte[] buffer = new byte[4096];
int bytes_read;
while ((bytes_read = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes_read);
}
} catch (Exception e) {
System.err.println(e);
} finally {
try {
in.close();
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
}我是在公司用代理服务器访问网络的,请问为什么我可以用IE访问csdn,但是上面的代码就无法访问?
报错:
java.net.ConnectException: Connection timed out: connect
java.lang.NullPointerException
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;public class HtmlSourceView {
public static void main(String[] args) {
InputStream in = null;
OutputStream out = null;
try {
URL url = new URL("http://www.csdn.net");
in = url.openStream();
if (args.length == 2) {
out = new FileOutputStream(args[1]);
} else
out = System.out;
byte[] buffer = new byte[4096];
int bytes_read;
while ((bytes_read = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes_read);
}
} catch (Exception e) {
System.err.println(e);
} finally {
try {
in.close();
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
}我是在公司用代理服务器访问网络的,请问为什么我可以用IE访问csdn,但是上面的代码就无法访问?
报错:
java.net.ConnectException: Connection timed out: connect
java.lang.NullPointerException
解决方案 »
- 队列中元素个数怎么算?
- package 和 import 复习
- ?????????JAVA 记事本的求救?
- 簡單問題
- 关于JAR
- 我是从 “china”下载的eclipse-SDK-3.1.1-win32.zip,为什么安装的时候还是英文的呢?
- 一道double hashing面试测验题,小弟不会,巨汗,望高手相救
- Tomcat5.0.x配置好了SSL之后,不能https://localhost/访问,而要这样访问 https://localhost:8443/ ,难道redir
- 如何再次激活对象?
- 那位大哥告诉我一些能下载java好书的网站!(万分感谢!)
- 请问JDBC的真正含义是什么
- lucene索引文件损坏的问题??
Proxy proxy=new Proxy(Proxy.Type.HTTP,new InetSocketAddress(host,port));
URL url=new URL(url);
URLConnection connection=url.openConnection(proxy);
connection.connect();
InputStream in=connection.getInputStream();
5.0之前需要设置系统代理属性
System.setProperty("proxySet","true");
System.setProperty("proxyHost",host);
System.setProperty("proxyPort",port);