从C#App调用Google Chart API时出现问题

时间:2011-05-24 13:37:35

标签: c# google-visualization

我想在Job或Winforms应用中生成QR码。

我的api电话很简单:

http://chart.apis.google.com/chart?cht=qr&chld=L|0&chs=80x80&chl=http%3a%2f%2futip.it.%2ft%2f1833

当我从浏览器中调用它时很好。

当我使用以下代码调用它时,出现超时错误:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Timeout = 5000;
request.ReadWriteTimeout = 5000;
request.ContentType = "image/png";
HttpWebResponse imgresponse = (HttpWebResponse)request.GetResponse();
System.Drawing.Image img = System.Drawing.Image.FromStream(imgresponse.GetResponseStream());

当我打开Fiddler调试请求时,它可以工作。我假设我需要在请求上设置一些标头。我尝试了一些但没有成功..

1 个答案:

答案 0 :(得分:0)

不明白为什么,但使用此代码有效:

    byte[] fileBytes = null;

    WebClient client = new WebClient();
    fileBytes = client.DownloadData(url);
    MemoryStream theMemStream = new MemoryStream();

    theMemStream.Write(fileBytes, 0, fileBytes.Length);
    System.Drawing.Image img2 = System.Drawing.Image.FromStream(theMemStream);