从 C# 应用程序调用 Google Chart API 时出现问题

发布于 2024-11-09 06:47:39 字数 665 浏览 4 评论 0原文

我想在 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 来调试请求时,它可以工作。我假设我需要在请求上设置一些标头。我尝试了一些但没有成功..

I want to generate a QR Code in a Job or Winforms app.

My api call is straightforward:

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

When I call it from the browser its fine.

When I call it using the following code, I get a timeout error:

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());

When I turn on Fiddler to debug the request, it works. I assume that I need to set some header on the request. I've tried a few but no success..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池予 2024-11-16 06:47:39

不太明白为什么,但使用这段代码是有效的:

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

Don't really understand why, but using this code works:

    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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文