/// <summary> /// We're about to render the page - search for the soils and write names to the /// response. The iPad soil app uses this method. /// </summary> protected void Page_PreRender(object sender, EventArgs e) { Response.Clear(); Response.ContentType = "text/plain"; Response.ContentEncoding = System.Text.Encoding.UTF8; if (Request.QueryString["Name"] != null) { string SoilName = Request.QueryString["Name"]; ApsoilWeb.Service SoilsDB = new Apsoil.ApsoilWeb.Service(); XmlDocument Doc = new XmlDocument(); Doc.LoadXml(SoilsDB.SoilXML(SoilName)); MemoryStream MemStream = new MemoryStream(10000); Doc.Save(MemStream); if (MemStream.ToArray() != null) { Response.BinaryWrite(MemStream.ToArray()); } } else if (Request.QueryString["NameJSON"] != null) { string SoilName = Request.QueryString["NameJSON"]; ApsoilWeb.Service SoilsDB = new Apsoil.ApsoilWeb.Service(); Response.Write(SoilsDB.SoilAsJson(SoilName)); } Response.End(); }
/// <summary>User has clicked show JSON button.</summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void ShowJSONClick(object sender, EventArgs e) { StringWriter writer = new StringWriter(); writer.WriteLine("["); using (ApsoilWeb.Service soilsDB = new Apsoil.ApsoilWeb.Service()) { List <string> selectedPaths = GetSelectedItems(); foreach (string path in selectedPaths) { writer.Write(soilsDB.SoilAsJson(path)); if (selectedPaths.Last() != path) { writer.WriteLine(","); } else { writer.WriteLine(""); } } } writer.WriteLine("]"); ShowString(writer.ToString()); }
/// <summary> /// Test method for creating a chart from JSON. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void OnSoilChartFromJson(object sender, EventArgs e) { string SelectedName = ListBox.SelectedValue; using (ApsoilWeb.Service soilsDB = new Apsoil.ApsoilWeb.Service()) { string json = soilsDB.SoilAsJson(SelectedName); byte[] bytes = Encoding.Default.GetBytes(json); HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("https://apsimdev.apsim.info/ApsoilWeb/SoilChart.aspx"); //Our method is post, otherwise the buffer (postvars) would be useless WebReq.Method = "POST"; //We use form contentType, for the postvars. WebReq.ContentType = "text/plain"; //The length of the buffer (postvars) is used as contentlength. WebReq.ContentLength = bytes.Length; //We open a stream for writing the postvars Stream PostData = WebReq.GetRequestStream(); //Now we write, and afterwards, we close. Closing is always important! PostData.Write(bytes, 0, bytes.Length); PostData.Close(); HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); Stream Answer = WebResp.GetResponseStream(); byte[] chartBytes = new byte[200000]; int numBytes = Answer.Read(chartBytes, 0, chartBytes.Length); Array.Resize(ref chartBytes, numBytes); Response.Clear(); Response.ContentType = "image/png"; Response.BinaryWrite(chartBytes); Response.Flush(); Response.End(); } }