访问客户端编程,下面是访问某网站返回的(HTML)代码:
例子1:
public class SocketClient {
Socket socket;
public SocketClient() {
}
public String SocketClientStr() {
String str = "";
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
String def = "gzip, ";
try {
URL urlmy = new URL("//http://www.***.com/");
HttpURLConnection con = (HttpURLConnection) urlmy.openConnection();
con.setFollowRedirects(true);
con.setInstanceFollowRedirects(true);
con.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
while((str = reader.readLine()) != null){
sb.append(str);
System.out.println(str);
str = reader.readLine();
}
socket.close();
in.close();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public static void main(String[] args) {
SocketClient n = new SocketClient();
System.out.println(n.SocketClientStr());
}
例子2:
public class SocketClient2 {
public static void main(String[] args){
try{
String sendHead = "GET / HTTP/1.1\r\n"
+ "Accept: */*\r\n"
+ "Accept-Language: zh-cn\r\n"
+ "Accept-Encoding: deflate\r\n"
+ "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"
+ "Host: www.baidu.com\r\n"
+ "Connection: Keep-Alive\r\n"
+ "Cookie: BAIDUID=75E25D4C8085E5BEE50DB1CCB97489FB:FG=1\r\n\r\n";
Socket socket=new Socket("121.14.88.76",80);
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline;
readline=sin.readLine(); //从系统标准输入读入一字符串
while(!readline.equals( "bye ")){
// os.println(readline);
// os.flush();
System.out.println( "Client: "+readline);
System.out.println( "Server: "+is.readLine());
readline=sin.readLine(); //从系统标准输入读入一字符串
}
os.close(); //关闭Socket输出流
is.close(); //关闭Socket输入流
socket.close(); //关闭Socket
}catch(Exception e) {
System.out.println("error is : " + e);
}
}
例子1:
public class SocketClient {
Socket socket;
public SocketClient() {
}
public String SocketClientStr() {
String str = "";
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
String def = "gzip, ";
try {
URL urlmy = new URL("//http://www.***.com/");
HttpURLConnection con = (HttpURLConnection) urlmy.openConnection();
con.setFollowRedirects(true);
con.setInstanceFollowRedirects(true);
con.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
while((str = reader.readLine()) != null){
sb.append(str);
System.out.println(str);
str = reader.readLine();
}
socket.close();
in.close();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public static void main(String[] args) {
SocketClient n = new SocketClient();
System.out.println(n.SocketClientStr());
}
例子2:
public class SocketClient2 {
public static void main(String[] args){
try{
String sendHead = "GET / HTTP/1.1\r\n"
+ "Accept: */*\r\n"
+ "Accept-Language: zh-cn\r\n"
+ "Accept-Encoding: deflate\r\n"
+ "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"
+ "Host: www.baidu.com\r\n"
+ "Connection: Keep-Alive\r\n"
+ "Cookie: BAIDUID=75E25D4C8085E5BEE50DB1CCB97489FB:FG=1\r\n\r\n";
Socket socket=new Socket("121.14.88.76",80);
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline;
readline=sin.readLine(); //从系统标准输入读入一字符串
while(!readline.equals( "bye ")){
// os.println(readline);
// os.flush();
System.out.println( "Client: "+readline);
System.out.println( "Server: "+is.readLine());
readline=sin.readLine(); //从系统标准输入读入一字符串
}
os.close(); //关闭Socket输出流
is.close(); //关闭Socket输入流
socket.close(); //关闭Socket
}catch(Exception e) {
System.out.println("error is : " + e);
}
}