加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET WebRequest到Highcharts导出服务器

发布时间:2020-12-16 06:32:17 所属栏目:asp.Net 来源:网络整理
导读:我需要在PDF报告中包含一个highcharts图表.但是如何获得export.highcharts.com生成的image / png? 这就是我到目前为止所做的事情: 单击按钮,将触发此ajax请求: $.ajax({ url: "Home/Teste",type: "POST",dataType: "html",data: { svgParam: myChart.getS
我需要在PDF报告中包含一个highcharts图表.但是如何获得export.highcharts.com生成的image / png?
这就是我到目前为止所做的事情:

单击按钮,将触发此ajax请求:

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

在服务器上,我得到请求和处理如下:

[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,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是一个html字符串,内容如下:“

我在asp.net中得到了这个svgParam而没有任何问题.但是来自export.highcharts.com的回复总是一样的,好像没有发送te svg:

<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>

为了测试,我在我的应用程序中创建了另一个方法,以接收此WebRequest,如下所示:

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

接收filenam,type和width参数.但是svg one是null.我试图编码,不编码,序列化为json字符串,更改内容类型……什么都没有,svg参数永远不会到达目的地.

有任何想法吗?

解决方法

如果您复制并粘贴了代码并且这不是印刷错误,则会将请求参数中的“svg”错误地发送到导出服务器.你有“sgv”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读