java--模拟浏览器

    xiaoxiao2025-06-03  42

    模拟一个浏览器客户端向服务器发请求,接收并显示响应消息。 import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.Socket; public class browse { public static void main(String[] args) throws IOException { Socket s=new Socket("www.hncu.net", 80); PrintWriter pw=new PrintWriter(s.getOutputStream(), true); pw.println("GET / HTTP/1.1"); pw.println("Accept: */*"); pw.println("Host: www.hncu.net"); pw.println("Connection: keep-alive"); pw.println();//必须要有一个空行 InputStream in=s.getInputStream(); byte buf[]=new byte[512]; int len=0; while((len=in.read(buf))!=-1){ String str=new String(buf,0,len,"utf-8"); System.out.println(str); } } }

    以下是请求的网页代码部分,会被如果myelipese像浏览器可以解析,这里则会显示www.hncu.net网站

    HTTP/1.1 200 OK Date: Sun, 14 Aug 2016 14:03:03 GMT Server: VWebServer X-Frame-Options: SAMEORIGIN Last-Modified: Thu, 11 Aug 2016 07:15:05 GMT ETag: “6f7c-539c6866ab040” Accept-Ranges: bytes Content-Length: 28540 Vary: User-Agent,Accept-Encoding Cache-Control: private, max-age=600 Expires: Sun, 14 Aug 2016 14:13:03 GMT Keep-Alive: timeout=5, max=200 Connection: Keep-Alive Content-Type: text/html Content-Language: zh-CN

    ?湖南城市学院

    转载请注明原文地址: https://ju.6miu.com/read-1299559.html
    最新回复(0)