/// <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; } } }
protected override bool SerializeToXmlElement(XmlWriter writer, string elementName) { bool flag = base.SerializeToXmlElement(writer, elementName); bool flag2 = this.OptionalIssuedTokenParameters.Count > 0; if (flag2 && (writer != null)) { MemoryStream w = new MemoryStream(); using (XmlTextWriter writer2 = new XmlTextWriter(w, Encoding.UTF8)) { writer2.Formatting = Formatting.Indented; writer2.WriteStartElement("alternativeIssuedTokenParameters"); foreach (IssuedTokenParametersElement element in this.OptionalIssuedTokenParameters) { element.SerializeToXmlElement(writer2, "issuedTokenParameters"); } writer2.WriteEndElement(); writer2.Flush(); string str = new UTF8Encoding().GetString(w.GetBuffer(), 0, (int)w.Length); writer.WriteComment(str.Substring(1, str.Length - 1)); writer2.Close(); } } if (!flag) { return(flag2); } return(true); }
/// <summary> /// SerializeObjectToXML /// </summary> /// <param name="item"></param> /// <returns></returns> private static string SerializeObjectToXML(object item) { try { string xmlText; Type objectType = item.GetType(); XmlSerializer xmlSerializer = new XmlSerializer(objectType); MemoryStream memoryStream = new MemoryStream(); using (XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8) { Formatting = Formatting.Indented }) { xmlSerializer.Serialize(xmlTextWriter, item); memoryStream = (MemoryStream)xmlTextWriter.BaseStream; xmlText = new UTF8Encoding().GetString(memoryStream.ToArray()); memoryStream.Dispose(); if (xmlText.Length > 14000) { xmlText = xmlText.Substring(0, 14000); } return(xmlText); } } catch (Exception e) { System.Diagnostics.Debug.Write(e.ToString()); return(null); } }
protected override bool SerializeToXmlElement(XmlWriter writer, String elementName) { bool writeMe = base.SerializeToXmlElement(writer, elementName); bool writeComment = this.OptionalIssuedTokenParameters.Count > 0; if (writeComment && writer != null) { MemoryStream memoryStream = new MemoryStream(); using (XmlTextWriter commentWriter = new XmlTextWriter(memoryStream, Encoding.UTF8)) { commentWriter.Formatting = Formatting.Indented; commentWriter.WriteStartElement(ConfigurationStrings.AlternativeIssuedTokenParameters); foreach (IssuedTokenParametersElement element in this.OptionalIssuedTokenParameters) { element.SerializeToXmlElement(commentWriter, ConfigurationStrings.IssuedTokenParameters); } commentWriter.WriteEndElement(); commentWriter.Flush(); string commentString = new UTF8Encoding().GetString(memoryStream.GetBuffer(), 0, (int)memoryStream.Length); writer.WriteComment(commentString.Substring(1, commentString.Length - 1)); commentWriter.Close(); } } return(writeMe || writeComment); }
/// <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); }