1  //web浏览器
 2             //浏览器本质的原理:浏览器向服务器发请求,服务器把请求的内容返回给浏览器,然后浏览器把返回的内容绘制成一个图形化的界面
 3             //Socket一种通讯交流的技术
 4             //qq用户把信息通过socket传给qqServer,然后qqServer通过Socket把信息返回给qq用户
 5             //创建一个Socket通讯,指定通讯格式是stream,通讯协议是TCP
 6 
 7             //首先需要自己搭建一个cassini服务器,用自己写的浏览器去请求
 8             Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
 9             //指定该socket 通讯链接的服务器 
10             socket.Connect(new DnsEndPoint("127.0.0.1", 8090));  //需要本地的cassini服务器
11             //new一个socket通讯流
12             using (Stream stream = new NetworkStream(socket))
13             //向socket通讯中写入请求
14             using (StreamWriter sw = new StreamWriter(stream))
15             {
16                 sw.WriteLine("GET /btml5.html HTTP/1.1");
17                 sw.WriteLine("Host: 127.0.0.1:8090");
18                 sw.WriteLine();//空行回车,表示指令结束
19             }
20             //从socket通讯中读取内容
21             using (Stream stream = new NetworkStream(socket))
22             using (StreamReader sr = new StreamReader(stream))
23             {
24                 string line;
25                 while ((line = sr.ReadLine()) != null)
26                 {
27                     line = sr.ReadLine();
28                     Console.WriteLine(line);
29                 }
30             }
31             socket.Disconnect(false);
32             Console.ReadKey();
web浏览器:向本机的Cassini服务器发请求并打印返回的内容

相关文章:

  • 2021-10-07
  • 2021-12-17
  • 2022-01-21
  • 2021-12-20
  • 2021-05-09
猜你喜欢
  • 2021-06-25
  • 2021-10-10
  • 2021-05-24
  • 2021-12-06
  • 2021-10-07
  • 2022-12-23
  • 2021-10-07
相关资源
相似解决方案