/// <summary> /// 发送所有请求头到服务器 /// </summary> /// <param name="req"></param> /// <param name="_stream"></param> private void OutHeader(RequestInfo req, Stream _stream) { var _out = new StreamWriter(_stream); _out.Write(String.Format("GET {0} HTTP/1.0 \r\n", req.Path)); var host = req.Host; if (req.Port != 80) { host += ":" + req.Port; } _out.Write(String.Format(HEADERFORMAT, "HOST", host)); _out.Write(String.Format(HEADERFORMAT, HtmlStaticCore.SKIPMARKHEAD, 1)); foreach (String key in req.Headers.Keys) { var lowerKey = key.ToLower(); if (lowerKey != "connection" && lowerKey != "host") { var val = req.Headers[key]; _out.Write(String.Format(HEADERFORMAT, key, val)); } } _out.Write(CONNECTION_CLOSE); _out.Write("\r\n"); _out.Flush(); _stream.Flush(); }
public Stream GenHTML(RequestInfo req) { using (var client = new TcpClient()) { client.ReceiveTimeout = TIMEOUT; client.Connect(req.Host, req.Port); using (var stream = client.GetStream()) { OutHeader(req, stream); return HttpParseHelp.ToGzip(stream); } } }
public Stream GenHTML(RequestInfo req) { var writer = new StringWriter(); HttpContext.Current.Server.Execute(req.Path, writer, true); return new MemoryStream(); }
/// <summary> /// 将HttpRequest转换为RequestInfo /// </summary> /// <param name="request"></param> /// <returns></returns> private RequestInfo ToRequestInfo(HttpRequest request) { var info = new RequestInfo(); info.Path = request.RawUrl; info.Host = request.Url.Host; info.Port = request.Url.Port; info.Headers = request.Headers; return info; }
/// <summary> /// 生成缓存并保存 /// </summary> /// <param name="context"></param> /// <param name="key"></param> /// <param name="info"></param> private void GenHtmlAndSave(RequestInfo info) { if (!GlobalGenHtmlState.Contains(info.Key)) { try { GlobalGenHtmlState.Add(info.Key); var html = GenHTML.GenHTML(info); if (html != null) { Store.Save(info.Key, html); } else { LogHelp.Warn("getHtml is null " + info.Path); } } catch (Exception e) { throw e; } finally { GlobalGenHtmlState.Remove(info.Key); } } }