import java.util.Properties;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.InetAddress;
import java.net.UnknownHostException;public class Proxy { public static void main(String args[])
{
Properties pro=System.getProperties();
pro.setProperty("http.proxyHost","186.15.63.2");
pro.setProperty("http.proxyPort","8080");
pro.setProperty("https.proxyHost","186.15.63.2");
pro.setProperty("https.proxyPort","443");
URL url=null;
/////////////////////////////////////////////////////////////////////////////////
InetAddress address=null;
try{
address=InetAddress.getByName("http://www.sina.com");
}catch(UnknownHostException e)
{
e.printStackTrace();
}
System.out.println(address.getAddress().toString());
try{
// InetAddress address=InetAddress.getByName("http://www.sina.com");
// System.out.println(address.getAddress().toString());
Socket socket = new Socket(address, 8080);
InputStream in=socket.getInputStream();
byte []b= new byte[1024];
while(in.read(b)>0)
{
System.out.println(new String(b));
}
}catch(IOException e1)
{
e1.printStackTrace();
}
//////////////////////////////////////////////////////////////////////////////////////////
/*
try{
url=new URL("https://www.sina.com");
}catch(MalformedURLException e1)
{
e1.printStackTrace();
}
URLConnection con=null;
try{
con=url.openConnection();
InputStream in=con.getInputStream();
byte []b= new byte[1024];
while(in.read(b)>0)
{
System.out.println(new String(b));
}
}catch(IOException e1)
{
e1.printStackTrace();
}
*/
}
}
/////////////////////////////////////////////////////////////////////////////////////为什么上面的代码 报下面的错
java.net.UnknownHostException: http://www.sina.com: http://www.sina.com
at java.net.InetAddress.getAllByName0(InetAddress.java:1128)
at java.net.InetAddress.getAllByName0(InetAddress.java:1098)
at java.net.InetAddress.getAllByName(InetAddress.java:1061)
at java.net.InetAddress.getByName(InetAddress.java:958)
at Proxy.main(Proxy.java:27)
Exception in thread "main" java.lang.NullPointerException
at Proxy.main(Proxy.java:33)/////////////////////////////////////////////////////////////而/*......*/ 注释掉的之间的代码却可以读出网页的内容呢?
两者有什么不同?如果用socket实现 该怎么办?
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.InetAddress;
import java.net.UnknownHostException;public class Proxy { public static void main(String args[])
{
Properties pro=System.getProperties();
pro.setProperty("http.proxyHost","186.15.63.2");
pro.setProperty("http.proxyPort","8080");
pro.setProperty("https.proxyHost","186.15.63.2");
pro.setProperty("https.proxyPort","443");
URL url=null;
/////////////////////////////////////////////////////////////////////////////////
InetAddress address=null;
try{
address=InetAddress.getByName("http://www.sina.com");
}catch(UnknownHostException e)
{
e.printStackTrace();
}
System.out.println(address.getAddress().toString());
try{
// InetAddress address=InetAddress.getByName("http://www.sina.com");
// System.out.println(address.getAddress().toString());
Socket socket = new Socket(address, 8080);
InputStream in=socket.getInputStream();
byte []b= new byte[1024];
while(in.read(b)>0)
{
System.out.println(new String(b));
}
}catch(IOException e1)
{
e1.printStackTrace();
}
//////////////////////////////////////////////////////////////////////////////////////////
/*
try{
url=new URL("https://www.sina.com");
}catch(MalformedURLException e1)
{
e1.printStackTrace();
}
URLConnection con=null;
try{
con=url.openConnection();
InputStream in=con.getInputStream();
byte []b= new byte[1024];
while(in.read(b)>0)
{
System.out.println(new String(b));
}
}catch(IOException e1)
{
e1.printStackTrace();
}
*/
}
}
/////////////////////////////////////////////////////////////////////////////////////为什么上面的代码 报下面的错
java.net.UnknownHostException: http://www.sina.com: http://www.sina.com
at java.net.InetAddress.getAllByName0(InetAddress.java:1128)
at java.net.InetAddress.getAllByName0(InetAddress.java:1098)
at java.net.InetAddress.getAllByName(InetAddress.java:1061)
at java.net.InetAddress.getByName(InetAddress.java:958)
at Proxy.main(Proxy.java:27)
Exception in thread "main" java.lang.NullPointerException
at Proxy.main(Proxy.java:33)/////////////////////////////////////////////////////////////而/*......*/ 注释掉的之间的代码却可以读出网页的内容呢?
两者有什么不同?如果用socket实现 该怎么办?
socket吧?不知道哪位能详细的讲一讲。
创建新的socket的时候,参数不能使用url的吧?有个笨方法可以用的,就是先创建一
个url,然后使用url.getHost方法得到主机名,再用这个主机名做参数创建socket就
可以了,不知道大家还有没有更好的方法。
上面的错误找到了
使用socket
要用OutputStream写要获取的东西 而url不要可以直接读
原因还是不知道我也是不太清楚两者之间的关系
我改了一下程序 用socket读出的是微软的出错页面 而用URL可以正常读出页面 是什么原因?
那位清楚 麻烦解释一下
先谢谢了