您现在的位置是:亿华云 > IT科技类资讯
Socket技术,实现Http协议和游戏服务器协议(Java代码)
亿华云2025-10-08 23:32:36【IT科技类资讯】3人已围观
简介复制publicclasstestHttpClient{ publicstaticvoidmain(String[]arg){ Socketsocket=n
复制public class testHttpClient { publicstatic void main(String[] arg){ Socket socket=new Socket(); try { socket.connect(new InetSocketAddress("www.baidu.com",技术 80), 300); OutputStream o=socket.getOutputStream(); //http协议字符串 包括包头和包体,http采用"\r\n"做为分割,实现包头除了第一排之外,协议戏服协议都是和游采用key:value方式来保存信息,b2b供应网一把服务器解锁提取 String requestStr="GET / HTTP/1.1\r\n" //第一排三个信息 分别是代码 1、请求方式(一把都会用两张get和post) 2、技术请求路径 3、云服务器提供商实现采用http1.1协议 +"Host: www.baidu.com\r\n" //申明Host //如果有其它hearder 字段信息可以防止在这里 +"\r\n" //这里表示包头结束 +"\r\n"; //这里表示包体结束(由于这个get没有任何的协议戏服协议结构体,如果需要向post那样传参 key1=value1&key2=value2,和游注意一定要在包头中增加"Content-Length:***"申明包体长度) o.write(requestStr.getBytes()); o.flush(); BufferedReader i= new BufferedReader(new InputStreamReader (socket.getInputStream())); String response=""; String readOneStr=null; while ((readOneStr=i.readLine())!=null){ response+=readOneStr+"\r\n"; } System.out.println(response); }catch (IOException ioe){ ioe.printStackTrace(); } } 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.
很赞哦!(76)