用谷歌浏览器请求 Sockt 监听的8044地址:
接收的报文如下:
GET /index.cshtml HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Accept-Language: zh-Hans-CN,zh-Hans;q=0.5
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299
Accept-Encoding: gzip, deflate
Host: localhost:8044
Connection: Keep-Alive
正则采用多行模式进行处理成一行一行的数据 : MatchCollection strli = Regex.Matches(str, @"^.+?(?= )", RegexOptions.Multiline);
第一行: 请求方式,请求的地址, 通讯协议
第二行: 接收数据格式
第三行: 语言
第四行: 请求的方式标记(浏览器,andriod, IOS)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public string DoRequestString(string str) 2 { 3 MatchCollection strli = Regex.Matches(str, @"^.+?(?= )", RegexOptions.Multiline); 4 if (strli.Count <= 0) 5 { 6 return "101"; 7 } 8 foreach (var item in strli) 9 { 10 if (item.ToString().ToUpper().Contains("HOST:")) // 处理请求地址,看请求是否是静态资源,可直接返回 11 { 12 13 } 14 if (item.ToString().ToUpper().Contains("ACCEPT")) //处理请求的类型,这里可以限定,例如做接口服务的,可以限定为"text/html" 15 { 16 if (!item.ToString().ToUpper().Contains("TEXT/HTML")) 17 { 18 return "402"; 19 } 20 } 21 if (item.ToString().ToUpper().Contains("GET")) // 处理请求的地址 22 { 23 string ss = Regex.Match(item.ToString(), "/.+?(?=\s)").ToString(); 24 } 25 } 26 return "404"; 27 }