C # based on the Watercress API query book information
Last Update:2018-07-17
Source: Internet
Author: User
First, create a BookInfo class:
Class BookInfo
{
private string _name; public string Name
{
get {return _name;}
set {_name = value;}
}
private string _author; public string Author
{
get {return _author;}
set {_author = value;}
}
private string _imageurl; public string Imageurl
{
get {return _imageurl;}
set {_imageurl = value;}
}
private string _summary; public string Summary
{
get {return _summary;}
set {_summary = value;}
}
private string _ISBN; public string ISBN
{
get {return _ISBN;}
set {_ISBN = value;}
}
private string _pages; public string Pages
{
get {return _pages;}
set {_pages = value;}
}
private string _price; public string Price
{
get {return _price;}
set {_price = value;}
}
private string _publisher; public string Publisher
{
get {return _publisher;}
set {_publisher = value;}
}
Then invoke the BookInfo class in the main program:
HttpWebRequest myrequest = null;
HttpWebResponse myhttpresponse = null;
String doubanurl = "http://api.douban.com/book/subject/isbn/";
Doubanurl = Doubanurl + Textbox10.text + "?";
Myrequest = (HttpWebRequest) webrequest.create (Doubanurl);
Myhttpresponse = (HttpWebResponse) myrequest.getresponse ();
StreamReader reader = new StreamReader (Myhttpresponse.getresponsestream ());
String xmldetail = reader. ReadToEnd ();
Reader. Close ();
Myhttpresponse.close ();
BookInfo books = new BookInfo ();
XmlDocument xml = new XmlDocument ();
Xml. Loadxml (Xmldetail);
XmlNamespaceManager nsmgr = new XmlNamespaceManager (XML. NameTable);
Nsmgr. AddNamespace ("db", "Http://www.w3.org/2005/Atom");
XmlElement root = XML. DocumentElement;
XmlNodeList nodes = root. SelectNodes ("/db:entry", nsmgr);
foreach (XmlNode nodedata in nodes)
{
foreach (XmlNode childnode in Nodedata.childnodes)
{
String str = Childnode. Name;
Switch (STR)
{
Case "title":
Books. Name = Childnode. InnerText;
Break
Case "link":
if (Childnode. ATTRIBUTES[1]. Value = = "image")
{
Books. Imageurl = Childnode. Attributes[0]. Value;
}
Break
Case "Summary":
Books. Summary = Childnode. InnerText;
Break
Case "Db:attribute":
{
Switch (childnode. Attributes[0]. Value)
{
Case "Isbn13":
Books. ISBN = Childnode. InnerText;
Break
Case "Pages":
Books. Pages = Childnode. InnerText;
Break
Case "Author":
Books. Author = Childnode. InnerText;
Break
Case "Price":
Books. Price = Childnode. InnerText;
Break
Case "publisher":
Books. Publisher = Childnode. InnerText;
Break
Case "pubdate":
Books. pubdate = Childnode. InnerText;
Break
}//end switch
Break
}
}//end switch
}//end foreach
TextBox1.Text = "title:" + Books. Name;
TextBox2.Text = "Author:" + Books. Author;
TextBox3.Text = "Cover:" + books. Imageurl;
Textbox4.text = "ISBN:" + books. ISBN;
Textbox5.text = "Pages:" + books. Pages;
Textbox6.text = "Price:" + books. Price;
Textbox7.text = "Publishing house:" + books. Publisher;
Textbox8.text = "Published date:" + books. pubdate;
Textbox9.text = "Introduction:" + Books. Summary;
Picturebox1.waitonload = false;
Picturebox1.loadasync (books. Imageurl);//Use asynchronous load book cover photo
Back to write a Baidu music grab class, to rewrite the previous one, haha