重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
封裝請求協議:獲取method uri以及請求參數
public class Request {
//協議信息
private String requestInfo;
//請求方式
private String method;
//請求的uri
private String url;
//請求的參數
private String queryStr;
public Request(InputStream is)
{
byte[] datas=new byte[1024*1024];
int len = 0;
try {
len=is.read(datas);
this.requestInfo =new String(datas,0,len);
System.out.println(requestInfo);
} catch (IOException e) {
e.printStackTrace();
return;
}
//分解字符串
parseRequestInfo();
}
public Request(Socket client) throws IOException
{
this(client.getInputStream()); //構造器的相互調用,只能放在首行
}
private void parseRequestInfo()
{
System.out.println("----分解----");
//獲取請求方式
System.out.println("1、獲取請求方式:開頭到第一個/之間");
this.method=this.requestInfo.substring(0,this.requestInfo.indexOf("/")).toLowerCase();
//獲取url
System.out.println("2、獲取url:第一個/到HTTP/第一個字母之間");
System.out.println("---可能包含請求參數?前面的為url");
//url:獲取第一個/的位置
int startIdx1=this.requestInfo.indexOf("/")+1; //要/之后的內容
//url:獲取HTTP/的第一個字母的位置
int endIdx2=this.requestInfo.indexOf("HTTP/"); //返回第一個字母的位置
//url:分割字符串
this.url=this.requestInfo.substring(startIdx1,endIdx2);
//獲取?的位置
int queryIdx=this.url.indexOf("?"); //大于零表示存在
if(queryIdx>0)
{
String[] urlArray=this.url.split("\\?");//必須轉義
this.url=urlArray[0];
queryStr=urlArray[1]; //請求參數
}
System.out.println(this.url);
}
}
Server:
public class Server04 {
private ServerSocket serverSocket ;
public static void main(String[] args) {
Server04 server = new Server04();
server.start();
}
//啟動服務
public void start() {
try {
serverSocket = new ServerSocket(8888);
receive();
} catch (IOException e) {
e.printStackTrace();
System.out.println("服務器啟動失敗....");
}
}
//接受連接處理
public void receive() {
try {
Socket client = serverSocket.accept();
System.out.println("一個客戶端建立了連接....");
//獲取請求協議
Request request=new Request(client);
//關注了內容
Response response=new Response(client); //創建好了輸出流
response.print(""); //通過輸出流輸出
response.print("");
response.print("");
response.print("服務器響應成功");
response.print(" ");
response.print("");
response.print("");
response.print("shsxt server終于回來了。。。。");
response.print("");
response.print("");
//關注了狀態碼
response.pushToBrowser(200);
} catch (IOException e) {
e.printStackTrace();
System.out.println("客戶端錯誤");
}
}
//停止服務
public void stop() {
}
}
Response:
public class Response {
private BufferedWriter bw;
//正文
private StringBuilder content;
//協議頭(狀態行與請求頭 回車)信息
private StringBuilder headInfo;
private int len; //正文的字節數
private final String BLANK =" ";
private final String CRLF = "\r\n";
private Response() {
content =new StringBuilder();
headInfo=new StringBuilder();
len =0;
}
public Response(Socket client) {
this();
try {
bw=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
headInfo = null;
}
}
public Response(OutputStream os) {
this();
bw=new BufferedWriter(new OutputStreamWriter(os));
}
//流模式動態添加內容
public Response print(String info) {
content.append(info);
len+=info.getBytes().length;
return this;
}
public Response println(String info) {
content.append(info).append(CRLF);
len+=(info+CRLF).getBytes().length;
return this;
}
//推送響應信息
public void pushToBrowser(int code) throws IOException {
if(null ==headInfo) {
code = 505;
}
createHeadInfo(code);
bw.append(headInfo);
bw.append(content);
bw.flush();
}
//構建頭信息
private void createHeadInfo(int code) {
//1、響應行: HTTP/1.1 200 OK
headInfo.append("HTTP/1.1").append(BLANK);
headInfo.append(code).append(BLANK);
switch(code) {
case 200:
headInfo.append("OK").append(CRLF);
break;
case 404:
headInfo.append("NOT FOUND").append(CRLF);
break;
case 505:
headInfo.append("SERVER ERROR").append(CRLF);
break;
}
//2、響應頭(最后一行存在空行):
headInfo.append("Date:").append(new Date()).append(CRLF);
headInfo.append("Server:").append("shsxt Server/0.0.1;charset=GBK").append(CRLF);
headInfo.append("Content-type:text/html").append(CRLF);
headInfo.append("Content-length:").append(len).append(CRLF);
headInfo.append(CRLF);
}
}
創新互聯www.cdcxhl.cn,專業提供香港、美國云服務器,動態BGP最優骨干路由自動選擇,持續穩定高效的網絡助力業務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統配攻擊溯源,準確進行流量調度,確保服務器高可用性。佳節活動現已開啟,新人活動云服務器買多久送多久。