开发者

Problem calling Google Chart API from C# App

开发者 https://www.devze.com 2023-03-08 12:26 出处:网络
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

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.Creat开发者_JS百科e(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..


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号