public HTTP1(string url, Encoding encode) { _encode = encode; _method = "GET"; _type = (int)EType.HTTP; _version = (int)EVersion.HTTP_11; IPPort data = AnalysisHelper.AnalysisIPAndPort(url); _destIP = data.IP; _destPort = data.Port; _virPath = data.VirtualPath; _boudary = DateTime.Now.Ticks.ToString("x"); _requestHeadData.Add("Host", data.Host); _requestHeadData.Add("User-Agent", "Mozilla / 5.0(Windows NT 10.0; WOW64; rv: 49.0) Gecko / 20100101 Firefox / 49.0"); _requestHeadData.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"); _requestHeadData.Add("Accept-Encoding", "gzip, deflate"); _requestHeadData.Add("Content-Type", "text/html"); //_requestHeadData.Add("Connection", "Keep-Alive"); //_requestHeadData.Add("Proxy-Connection", "Keep-Alive"); PacketData(); }
/// <summary> /// 解析IP /// </summary> /// <param name="url">地址</param> internal static IPPort AnalysisIPAndPort(string url) { IPPort data = new IPPort(); string host = ""; Regex reg = new Regex(_patternIP); Match match = reg.Match(url); bool appointPort = true; //url中是否指定端口 int index = -1; //虚拟路径在url中起始索引 if (match.Success) { //IP形式url data.IP = match.Value; host = data.IP;//IP作为主机 index = match.Index + match.Value.Length; //匹配IP+端口 reg = new Regex(_patternIP + _patternPort); match = reg.Match(url); if (match.Success) { data.Port = Convert.ToInt32(match.Value.Substring(match.Value.IndexOf(":") + 1)); index += 1 + data.Port.ToString().Length;//: + 端口的总长度 } else { appointPort = false; } } else { //域名形式url reg = new Regex(_patternDomain); match = reg.Match(url); if (match.Success) { host = match.Value;//域名作为主机 index = match.Index + match.Value.Length; data.IP = AnalysisDomain(match.Value); //匹配域名+端口 reg = new Regex(_patternDomain + _patternPort); match = reg.Match(url); if (match.Success) { data.Port = Convert.ToInt32(match.Value.Substring(match.Value.IndexOf(":") + 1)); index += 1 + data.Port.ToString().Length;//: + 端口的总长度 } else { appointPort = false; } } } //url中没有指定端口,采用默认端口 if (!appointPort) { data.Port = 80; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { data.Port = 443; } data.Host = host; } else { //url中指定了端口 data.Host = string.Format("{0}:{1}", host, data.Port); } if (index < url.Length) { data.VirtualPath = url.Substring(index); } else { data.VirtualPath = "/"; } #if DEBUG Debug.WriteLine(string.Format("Mqd.HTTPHelper: 目标IP = {0} 目标端口 = {1}", data.IP, data.Port)); #endif return(data); }