📄 simplewebclient.java
字号:
import java.io.*;
import java.net.*;
//一个简单的从服务器取回一个HTML页面的程序
public class SimpleWebClient{
public static void main(String args[]) {
try{//打开一个客户端socket连接
Socket clientSocket=new Socket("127.0.0.1",8080);
System.out.println("Client: " + clientSocket);
//取得一个网页
getPage(clientSocket);
} catch(UnknownHostException uhe) {
System.out.println("UnknownHostException:"+uhe);
} catch (IOException ioe) {
System.err.println("IOException: " + ioe);
}
}
/**
*通过建立的连接请求一个页面,显示回应然后关闭socket
*/
public static void getPage(Socket clientSocket) {
try { //需要输入和输出流
DataOutputStream outbound=new DataOutputStream(
clientSocket.getOutputStream());
DataInputStream inbound =new DataInputStream(
clientSocket.getInputStream() );
//向服务器发出HTTP请求
outbound.writeBytes("GET /HTTP/1.0\r\n\r\n");
//读出回应
String responseLine;
while((responseLine=inbound.readLine())!=null) {
//把每一行显示出来
System.out.println(responseLine);
if(responseLine.indexOf("")!=-1)
break;
}
//清除
outbound.close();
inbound.close();
clientSocket.close();
} catch(IOException ioe) {
System.out.println("IOException:"+ioe);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -