public const string TGW_STRING_HEADER = "tgw_l7_forward\r\n";//这个是固定的 /// <summary> /// 处理TGW(腾讯防火墙)的请求 /// </summary> private bool HandleTGWRequest(byte[] buffer, int offset, int count, ref int headerLength) { DataHelper.SortBytes(buffer, offset, count); string cmdData = new UTF8Encoding().GetString(buffer, offset, count); if (0 == cmdData.IndexOf(TGW_STRING_HEADER)) { int findEnd = cmdData.IndexOf("\r\n\r\n"); if (findEnd > 0) { findEnd += 4; headerLength = findEnd; //清空当前的状态 _PacketCmdID = 0; _PacketDataSize = 0; PacketDataHaveSize = 0; IsWaitingData = false; CmdHeaderSize = 0; _LastCheckTicks = 0; DataHelper.SortBytes(buffer, offset, count); return(true); } } DataHelper.SortBytes(buffer, offset, count); return(false); }
/// <summary> /// 显示价格的方法 /// </summary> /// <param name="canToken"></param> void ShowStockPrice(CancellationToken canToken) { TaskStartTime = DateTime.Now; while (true) { try { WebClient wbc = new WebClient(); byte[] bdata = wbc.DownloadData("http://www.nasdaq.com/aspx/infoquotes.aspx?symbol=IBM&selected=IBM"); string pageText = new UTF8Encoding().GetString(bdata); int pos1 = pageText.IndexOf("LastSale1'>"); int pos2 = pageText.IndexOf("</", pos1 + 1); string price = "Price:" + pageText.Substring(pos1 + 52, pos2 - pos1 - 60); showPriceWindow.ChangeText1(price); priceChanged(price); canToken.WaitHandle.WaitOne(30000); canToken.ThrowIfCancellationRequested(); } catch (Exception) { return; } } }
/// <inheriteddoc /> public void ProcessRequest(HttpContext context) { try { // check login ICloudPrincipal loggedInUser = null; { var data = (context.Request.ServerVariables["HTTP_AUTHORIZATION"] ?? string.Empty).Trim(); if (string.IsNullOrWhiteSpace(data) == false) { if (data.ToLower().StartsWith("basic ")) { try { var base64EncodedData = data.Substring(data.IndexOf(" ")).Trim(); var blobData = Convert.FromBase64String(base64EncodedData); var strData = new UTF8Encoding().GetString(blobData); var semicolon = strData.IndexOf(":"); if (semicolon > -1) { var username = strData.Substring(0, semicolon).ToLower().Trim(); if (username == string.Empty) { username = null; } string pwd = null; if (semicolon < (strData.Length - 1)) { pwd = strData.Substring(semicolon + 1); } if (string.IsNullOrEmpty(pwd)) { pwd = null; } loggedInUser = context.GetPrincipalRepository() .TryFindPrincipalByLogin(username, pwd); } } catch { // ignore errors here } } } } if (loggedInUser == null) { context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; context.Response.AddHeader("WWW-Authenticate", "BASIC Realm=Cloud.NET"); return; } var req = new CloudRequest() { Context = context, Principal = loggedInUser, }; req.Context.Response.StatusCode = (int)HttpStatusCode.OK; this.OnProcessRequest(req); } catch (Exception ex) { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; context.Response.StatusDescription = (ex.GetBaseException() ?? ex).Message ?? string.Empty; } finally { context.Response.End(); } }
public static HTTPMessage ParseByteArray(byte[] buffer, int indx, int count) { byte[] buffer2; HTTPMessage message = new HTTPMessage(); string str = new UTF8Encoding().GetString(buffer, indx, count); DText text = new DText(); int index = str.IndexOf("\r\n\r\n"); str = str.Substring(0, index); text.ATTRMARK = "\r\n"; text.MULTMARK = ":"; text[0] = str; string str3 = text[1]; DText text2 = new DText(); text2.ATTRMARK = " "; text2.MULTMARK = "/"; text2[0] = str3; if (str3.ToUpper().StartsWith("HTTP/")) { message.ResponseCode = int.Parse(text2[2]); int startIndex = str3.IndexOf(" "); startIndex = str3.IndexOf(" ", (int)(startIndex + 1)); message.ResponseData = UnEscapeString(str3.Substring(startIndex)); try { message.Version = text2[1, 2]; } catch (Exception) { message.Version = "0.9"; } } else { message.Directive = text2[1]; string theString = str3.Substring(str3.LastIndexOf(" ") + 1); if (!theString.ToUpper().StartsWith("HTTP/")) { message.Version = "0.9"; message.DirectiveObj = UnEscapeString(theString); } else { message.Version = theString.Substring(theString.IndexOf("/") + 1); int num3 = str3.IndexOf(" ") + 1; message.DirectiveObj = UnEscapeString(str3.Substring(num3, ((str3.Length - num3) - theString.Length) - 1)); } } string tagName = ""; string tagData = ""; for (int i = 2; i <= text.DCOUNT(); i++) { if ((tagName != "") && text[i, 1].StartsWith(" ")) { tagData = text[i, 1].Substring(1); } else { tagName = text[i, 1]; tagData = ""; for (int j = 2; j <= text.DCOUNT(i); j++) { if (tagData == "") { tagData = text[i, j]; } else { tagData = tagData + text.MULTMARK + text[i, j]; } } } message.AppendTag(tagName, tagData); } int length = 0; if (message.HasTag("Content-Length")) { try { length = int.Parse(message.GetTag("Content-Length")); } catch (Exception) { length = -1; } } else { length = -1; } if (length > 0) { buffer2 = new byte[length]; if (((index + 4) + length) <= count) { Array.Copy(buffer, index + 4, buffer2, 0, length); message.DataBuffer = buffer2; } } switch (length) { case -1: buffer2 = new byte[count - (index + 4)]; Array.Copy(buffer, index + 4, buffer2, 0, buffer2.Length); message.DataBuffer = buffer2; break; case 0: message.DataBuffer = new byte[0]; break; } return(message); }