如何解决从C#App调用Google Chart API时出现问题
| 我想在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调试请求时,它可以工作。我假设我需要在请求上设置一些标头。我尝试了一些但没有成功。
解决方法
不太清楚为什么,但是使用下面的代码是可行的:
byte[] fileBytes = null;
WebClient client = new WebClient();
fileBytes = client.DownloadData(url);
MemoryStream theMemStream = new MemoryStream();
theMemStream.Write(fileBytes,fileBytes.Length);
System.Drawing.Image img2 = System.Drawing.Image.FromStream(theMemStream);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。