ASP.NET WebRequest for Highcharts Export Server

I need to include a high graph chart in a PDF report. But how can I get the image / png created by export.highcharts.com? This is what I have done so far:

When a button is clicked, this ajax request is launched:

$.ajax({ url: "Home/Teste", type: "POST", dataType: "html", data: { svgParam: myChart.getSVG() }, success: function (data) { doStuff(data); }}); 

On the server, I receive a request and process:

 [HttpPost] [ValidateInput(false)] public void Teste(string svgParam) { // Create a request using a URL that can receive a post. HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://export.highcharts.com/"); // Set the Method property of the request to POST. request.Method = "POST"; // Create POST data and convert it to a byte array. string postData = string.Format("filename={0}&type={1}&width={2}&sgv={3}", "chart", "image/png", 1270, Server.UrlEncode(svgParam)); byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded; multipart/form-data"; //User agent is based in a normal export.js request request.UserAgent = @"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0"; // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. WebResponse response = request.GetResponse(); HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse(); //This is here just to read the response. string msg; using (StreamReader sReader = new StreamReader(webResponse.GetResponseStream())) { msg = sReader.ReadToEnd(); } } 

svgParam is an html line with content like this:

I get this svgParam in asp.net without any problems. But the answer from export.highcharts.com is always the same, as if te svg had not been sent:

 <body> <div id="top"> <a href="http://www.highcharts.com" title="Highcharts Home Page" id="logo"><img alt="Highcharts Home Page" src="resources/Highcharts-icon-160px.png" border="0"></a> <h1>Highcharts Export Server</h1> </div> <div id="wrap"> <h3>Oops..,</h3> <p>The manadatory svg POST parameter is undefined.</p> </div> </body> 

For testing, I created another method in my application to get this WebRequest as follows:

 [HttpPost] [ValidateInput(false)] public void Teste2(string filename, string type, string width, string svg) { string whereIsMySvg = svg; } 

Received parameters filenam, type and width. But svg one is null. I tried to encode, not encode, serialize as a json string, change the content type ... and nothing, the svg parameter never gets to the destination.

Any ideas?

+4
source share
1 answer

If you copied and pasted your code, and this is not a typo, you incorrectly spelled "svg" in the request parameters sent to the exporting server. You have "sgv".

+7
source

All Articles