当前位置: 技术问答>java相关
java socket 问题
来源: 互联网 发布时间:2015-02-20
本文导语: 我想用下面一段程序的到sohu首页的源代码,为何不行?谢谢! import java.io.*; import java.net.*; import java.util.*; public class http{ private static final int HTTP_PORT=80; public static void main(String args[]){ ...
我想用下面一段程序的到sohu首页的源代码,为何不行?谢谢!
import java.io.*;
import java.net.*;
import java.util.*;
public class http{
private static final int HTTP_PORT=80;
public static void main(String args[]){
String host="www.sohu.com";
InetAddress hostaddress;
Socket socket;
BufferedReader socketinput;
DataOutputStream socketoutput;
try{
hostaddress=InetAddress.getByName(host);
System.out.println("正在连接服务器"+hostaddress+"..."+"rn");
socket=new Socket(hostaddress,HTTP_PORT);
socket.setSoTimeout(10000);
try{
socketinput=new BufferedReader(new InputStreamReader(socket.getInputStream()));
socketoutput=new DataOutputStream(socket.getOutputStream());
socketoutput.writeBytes("GET / /HTTP/1.1 rn");
while(socketinput.readLine()!=null){
System.out.println(socketinput.readLine());
continue;
}
}finally{
socket.close();
}
}
catch(Exception e){
System.out.println("......出现错误!");
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class http{
private static final int HTTP_PORT=80;
public static void main(String args[]){
String host="www.sohu.com";
InetAddress hostaddress;
Socket socket;
BufferedReader socketinput;
DataOutputStream socketoutput;
try{
hostaddress=InetAddress.getByName(host);
System.out.println("正在连接服务器"+hostaddress+"..."+"rn");
socket=new Socket(hostaddress,HTTP_PORT);
socket.setSoTimeout(10000);
try{
socketinput=new BufferedReader(new InputStreamReader(socket.getInputStream()));
socketoutput=new DataOutputStream(socket.getOutputStream());
socketoutput.writeBytes("GET / /HTTP/1.1 rn");
while(socketinput.readLine()!=null){
System.out.println(socketinput.readLine());
continue;
}
}finally{
socket.close();
}
}
catch(Exception e){
System.out.println("......出现错误!");
}
}
}
|
我想了想.
觉得用套接字,根据http协议获取网页内容是可行的.
但是有对这一程序来说就有很大的局限.
因为:
The most common form of Request-URI is that used to identify a
resource on an origin server or gateway. In this case the absolute
path of the URI MUST be transmitted (see section 3.2.1, abs_path) as
the Request-URI, and the network location of the URI (net_loc) MUST
be transmitted in a Host header field. For example, a client wishing
to retrieve the resource above directly from the origin server would
create a TCP connection to port 80 of the host "www.w3.org" and send
the lines:
GET /pub/WWW/TheProject.html HTTP/1.1
Host: www.w3.org
followed by the remainder of the Request. Note that the absolute path
cannot be empty; if none is present in the original URI, it MUST be
given as "/" (the server root).
所以你的socketoutput.writeBytes("GET / /HTTP/1.1 rn");
对于sohu是可行的,但是对于其它网站就不一定了.
所以我建议用另一种方法.
import java.io.*;
import java.net.*;
import java.util.*;
public class GetPage{
public static void main(String args[]){
String host="http://www.sina.com.cn/";
try{
URL address=new URL(/tech-qa-java/host/index.html);
URLConnection connection=address.openConnection();
connection.setUseCaches(true);
connection.setDoOutput(true);
BufferedReader in =new BufferedReader(new InputStreamReader(connection.getInputStream()));
while(in.readLine()!=null){
System.out.println(in.readLine());
}
}
catch(Exception e){
System.out.println("Error");
}
}
}
觉得用套接字,根据http协议获取网页内容是可行的.
但是有对这一程序来说就有很大的局限.
因为:
The most common form of Request-URI is that used to identify a
resource on an origin server or gateway. In this case the absolute
path of the URI MUST be transmitted (see section 3.2.1, abs_path) as
the Request-URI, and the network location of the URI (net_loc) MUST
be transmitted in a Host header field. For example, a client wishing
to retrieve the resource above directly from the origin server would
create a TCP connection to port 80 of the host "www.w3.org" and send
the lines:
GET /pub/WWW/TheProject.html HTTP/1.1
Host: www.w3.org
followed by the remainder of the Request. Note that the absolute path
cannot be empty; if none is present in the original URI, it MUST be
given as "/" (the server root).
所以你的socketoutput.writeBytes("GET / /HTTP/1.1 rn");
对于sohu是可行的,但是对于其它网站就不一定了.
所以我建议用另一种方法.
import java.io.*;
import java.net.*;
import java.util.*;
public class GetPage{
public static void main(String args[]){
String host="http://www.sina.com.cn/";
try{
URL address=new URL(/tech-qa-java/host/index.html);
URLConnection connection=address.openConnection();
connection.setUseCaches(true);
connection.setDoOutput(true);
BufferedReader in =new BufferedReader(new InputStreamReader(connection.getInputStream()));
while(in.readLine()!=null){
System.out.println(in.readLine());
}
}
catch(Exception e){
System.out.println("Error");
}
}
}
|
try this
Properties props = System.getProperties();
props.put("http.proxyHost", "proxyhostname");
props.put("http.proxyPort", "proxyhostport");
Properties props = System.getProperties();
props.put("http.proxyHost", "proxyhostname");
props.put("http.proxyPort", "proxyhostport");