← Tutti gli articoli
Book Information Retrieval ( Isbn ) with WebRequest on IsbnDb
10 April 2011 ·
Asp.Net · Article ·
927 visite
ISBNdb.com project is a database of books providing on-line and remote research tools for individuals, book stores, librarians, scientists, etc. Taking data from hundreds of libraries across the world ISBNdb is a unique tool you won't find anywhere else.
The ISBNdb.com provides an API that allows client requests to the ISBNdb.com server asking to return some data. The request is self consistent and does not rely on any previous history, it includes everything required for the server to produce a response. After the server processes the request and sends out a response the session ends -- in fact the next response may be handled by a different server behind the scenes (as a side note, technically, your application can use keep-alive mechanism to keep the communication channel up between sessions -- but each message exchange in such channel is nonetheless completely independent).
The books collection provides information about books. You can get to a set of books by keywords, subjects, ISBNs, etc
In the code below I search on ISBN, value is set the ISBN you are interested in. For example:
/api/books.xml?access_key=Z&index1=isbn&value1=0061031321
The returned results subset consists of zero or at most one member for this search type, ISBN records are unique in the database then we have the following Xml information:
- <?xml version="1.0" encoding="UTF-8"?>
- <ISBNdb server_time="2011-04-09T20:17:50Z">
- <BookList total_results="1" page_size="10" page_number="1" shown_results="1">
- <BookData book_id="maximizing_performance_and_scalability_with_ibm_websphere" isbn="1590591305" isbn13="9781590591307">
- <Title>Maximizing Performance and Scalability with IBM WebSphere</Title>
- <TitleLong></TitleLong>
- <AuthorsText>Adam G. Neat</AuthorsText>
- <PublisherText publisher_id="apress">APress</PublisherText>
- </BookData>
- </BookList>
- </ISBNdb>
I convert this xml in a readable format (See below).
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Net;
using
System.IO;
using
System.Xml;
using
System.Configuration;
public
partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSearch_Click(object sender, EventArgs e)
{
WebRequest req = null;
WebResponse rsp = null;
bool IsRequestSuccessfully;
try
{
string accesskey = ConfigurationManager.AppSettings["accesskey"];
//RepKover™, a durable and flexible lay-flat binding. ISBN: 978-0-596-80578-4
string uri = "http://isbndb.com/api/books.xml?access_key=" + accesskey + "&index1=isbn&value1=" + txtIsbn.Text;
req =
WebRequest.Create(uri); //Server.HtmlEncode(uri));
// WebProxy wp = new WebProxy("192.168.1.100", 80); // ; //"sm24ris", 80);
//wp.UseDefaultCredentials = true;
//wp.BypassProxyOnLocal = true;
//req.Proxy = wp; // WebProxy.GetDefaultProxy(); // Enable if using proxy
//req.Proxy = WebProxy.GetDefaultProxy();
req.Method =
"GET"; // Post method
req.ContentType =
"text/xml"; // content type
WebResponse response = req.GetResponse();
//StreamReader reader = new StreamReader(req.GetRequestStream());
StreamReader reader = new StreamReader(response.GetResponseStream());
//string text = reader.ReadToEnd();
XmlDocument xml = new XmlDocument();
xml.LoadXml(reader.ReadToEnd());
txtTitle.Text= GetNodeValue(
ref xml, "/ISBNdb/BookList/BookData/Title");
txtTitleLong.Text = GetNodeValue(
ref xml, "/ISBNdb/BookList/BookData/TitleLong");
txtAuthors.Text = GetNodeValue(
ref xml, "/ISBNdb/BookList/BookData/AuthorsText");
txtPublishers.Text = GetNodeValue(
ref xml, "/ISBNdb/BookList/BookData/PublisherText");
XmlNode xNodePublisherText = xml.SelectSingleNode("/ISBNdb/BookList/BookData/PublisherText");
txtPublisherId.Text = xNodePublisherText.Attributes[
"publisher_id"].Value;
txtResult.Text =
"OK"; // sw.ToString();
string r = "";
IsRequestSuccessfully =
true;
}
catch (WebException webEx)
{
txtResult.Text = webEx.ToString();
IsRequestSuccessfully =
false;
}
catch (Exception ex)
{
txtResult.Text = ex.ToString();
IsRequestSuccessfully =
false;
}
finally
{
//if (rsp != null) rsp.GetResponseStream().Close();
}
if (IsRequestSuccessfully==false)
{
txtTitle.Text =
"";
txtTitleLong.Text =
"";
txtAuthors.Text =
"";
txtPublishers.Text =
"";
txtPublisherId.Text =
"";
}
}
private string GetNodeValue(ref XmlDocument xmlDoc, String xPath)
{
String sValue = "";
try {
XmlNode xNode = xmlDoc.SelectSingleNode(xPath);
if (xNode != null)
sValue = xNode.InnerText.Trim();
}
catch (Exception ex)
{ }
return sValue;
}
private void SetNodeValue(ref XmlDocument xmlDoc, String xPath, String sValue)
{
try { XmlNode xNode = xmlDoc.SelectSingleNode(xPath);
if (xNode != null)
xNode.InnerText = sValue;
}
catch (Exception ex)
{ }
}
}