• 大小: 66KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-04
  • 语言: 其他
  • 标签:

资源简介

使用豆瓣API,根据书籍ISBN码,获取该本书籍的详细信息

资源截图

代码片段和文件信息

using System;
using System.IO;
using System.Net;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Web.script.Serialization;

namespace BookAPI
{
    static class BookApi
    {
        //根据ISBN码从豆瓣API获取书籍详细信息
        public static bool getInfo(string isbn out BookInfo bookInfo out string json)
        {
            try
            {
                //豆瓣API
                string uri = “https://api.douban.com/v2/book/isbn/“ + isbn;
                //获取书籍详细信息,Json格式
                json = doGet(uri “utf-8“);
                //将获取到的Json格式的文件转换为定义的类
                bookInfo = toMap(json);
                return true;
            }
            catch
            {
                //信息获取失败
                bookInfo = null;
                json = ““;
                return false;
            }
        }

        //HTTP的GET请求,获取书籍详细信息
        private static string doGet(string url string charset)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = “GET“;
            request.ContentType = “text/html;charset=UTF-8“;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response != null)
            {
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream System.Text.Encoding.GetEncoding(charset));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;
            }
            throw new Exception();
        }

        //HTTP获取图片,获取书籍封面
        public static Image doGetImage(string url)
        {
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            if (response != null)
            {
                Stream stream = response.GetResponseStream();
                return Image.FromStream(stream);
            }
            return drawCover();
        }

        //绘制封面
        private static Bitmap drawCover()
        {
            Bitmap image = new Bitmap(102 145);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.Silver);
            StringFormat format = new StringFormat { Alignment = StringAlignment.Center };
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            //封面显示内容:暂无封面
            g.DrawString(“暂无封面“ new Font(“黑体“ 12f Fontstyle.Regular) Brushes.Black 51 50 format);
            return image;
        }

        //Json解析
        private static BookInfo toMap(string jsonString)
        {
            //实例化javascriptSerializer类的新实例
            javascriptSerializer jss = new javascriptSerializer();
            try
            {
                //将指定的 JSON 字符串

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-01-08 15:39  BookAPI\
     目录           0  2017-01-08 16:41  BookAPI\BookAPI\
     文件         187  2017-01-08 15:39  BookAPI\BookAPI\App.config
     文件        3883  2017-01-08 16:32  BookAPI\BookAPI\BookAPI.csproj
     文件        4352  2017-01-08 16:37  BookAPI\BookAPI\BookApi.cs
     文件       15876  2017-01-08 16:37  BookAPI\BookAPI\Form1.Designer.cs
     文件        3336  2017-01-08 16:41  BookAPI\BookAPI\Form1.cs
     文件        5817  2017-01-08 16:37  BookAPI\BookAPI\Form1.resx
     文件         519  2017-01-08 15:39  BookAPI\BookAPI\Program.cs
     目录           0  2017-01-08 15:39  BookAPI\BookAPI\Properties\
     文件        1338  2017-01-08 15:39  BookAPI\BookAPI\Properties\AssemblyInfo.cs
     文件        2866  2017-01-08 15:39  BookAPI\BookAPI\Properties\Resources.Designer.cs
     文件        5612  2017-01-08 15:39  BookAPI\BookAPI\Properties\Resources.resx
     文件        1094  2017-01-08 15:39  BookAPI\BookAPI\Properties\Settings.Designer.cs
     文件         249  2017-01-08 15:39  BookAPI\BookAPI\Properties\Settings.settings
     目录           0  2017-01-08 15:39  BookAPI\BookAPI\bin\
     目录           0  2017-01-08 16:32  BookAPI\BookAPI\bin\Debug\
     文件       17920  2017-01-08 16:41  BookAPI\BookAPI\bin\Debug\BookAPI.exe
     文件         187  2017-01-08 15:39  BookAPI\BookAPI\bin\Debug\BookAPI.exe.config
     文件       32256  2017-01-08 16:41  BookAPI\BookAPI\bin\Debug\BookAPI.pdb
     文件       23168  2017-01-08 16:41  BookAPI\BookAPI\bin\Debug\BookAPI.vshost.exe
     文件         187  2017-01-08 15:39  BookAPI\BookAPI\bin\Debug\BookAPI.vshost.exe.config
     文件         490  2016-07-16 19:44  BookAPI\BookAPI\bin\Debug\BookAPI.vshost.exe.manifest
     目录           0  2017-01-08 15:39  BookAPI\BookAPI\obj\
     目录           0  2017-01-08 16:41  BookAPI\BookAPI\obj\Debug\
     文件         180  2017-01-08 16:37  BookAPI\BookAPI\obj\Debug\BookAPI.Form1.resources
     文件         180  2017-01-08 16:32  BookAPI\BookAPI\obj\Debug\BookAPI.Properties.Resources.resources
     文件         627  2017-01-08 16:41  BookAPI\BookAPI\obj\Debug\BookAPI.csproj.FileListAbsolute.txt
     文件         977  2017-01-08 16:37  BookAPI\BookAPI\obj\Debug\BookAPI.csproj.GenerateResource.Cache
     文件        2367  2017-01-08 16:32  BookAPI\BookAPI\obj\Debug\BookAPI.csprojResolveAssemblyReference.cache
     文件       17920  2017-01-08 16:41  BookAPI\BookAPI\obj\Debug\BookAPI.exe
............此处省略9个文件信息

评论

共有 条评论

相关资源