?? 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 + -