ISBNDB没有书籍结果,有效ISBN

时间:2015-08-13 23:31:21

标签: c# xml tcp-ip tcplistener

我在这里遇到了一个奇怪的问题。我将从解释我的计划开始:

我有一个C#应用程序。该计划的主要目标是根据其ISBN获取有关图书的信息。 ISBN通过Android设备上的TCP / IP扫描程序传递给程序。然后将ISBN放入有效的URL中,该URL用于从ISBNDB.com获取XML数据。

我遇到的问题是:

当我查询输入到TextBox的ISBN时,程序运行正常。当我查询从阅读器扫描的ISBN时,它会返回“没有结果”'

我已经尝试了各种方法来尝试并深入了解此案例。在读取XML之前,我有一个消息框显示它收到的XML: Messagebox showing XML
如您所见,它没有显示任何结果。但是,当我访问该网址时(也是从该计划中获得的):
MessageBox showing URL
我在Microsoft Edge中得到此信息:
enter image description here
哪个,正是我认为该应用程序也会得到的。

有谁知道发生了什么事?如果是这样,我该怎么做才能修复它以及如何改进我的代码以消除此错误?

对于有兴趣的人,这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Threading;
using System.Diagnostics;

namespace LibraryBookLister
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string XML = "";
        private void btnQuery_Click(object sender, EventArgs e)
        {

            GetXMLBarcodeData();

        }

        private void GetXMLBarcodeData()
        {
            string Barcode4 = textBarcode.Text;
            MessageBox.Show(Barcode4);
            string barcode = Barcode4;
            StringBuilder output = new StringBuilder();

            XmlUrlResolver resolver = new XmlUrlResolver();
            resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

            // Set the reader settings object to use the resolver.
            if(barcode.Length > 13)
            {
                barcode = barcode.Remove(14);
                MessageBox.Show(barcode);
            }
            string xmlString = @"?access_key=IDC057UX&results=details&index1=isbn&value1=" + barcode;
            MessageBox.Show("GEttting book info for : "  + barcode);

            Uri baseUri = new Uri("https://isbndb.com/api/books.xml");
            Uri fulluri = resolver.ResolveUri(baseUri, xmlString);
            MessageBox.Show("Now Getting The URL: " + fulluri.ToString());
            Process.Start(fulluri.ToString());


            StringBuilder sb = new StringBuilder();
            XmlReader readesr = XmlReader.Create(fulluri.ToString());

            MessageBox.Show("REading data from " + fulluri.ToString());
            while (readesr.Read())
            {

                sb.AppendLine(readesr.ReadOuterXml());

            }
            string XMLs = sb.ToString();
            XML = XMLs;
            MessageBox.Show("XML : " + XML);
            GetXMLStuff();
        }
        public void GetXMLStuff()
        {
            tcplistener.Stop();
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(XML);

                        XmlNodeList nodes = doc.DocumentElement.SelectNodes("/ISBNdb/BookList");

            List<Book> books = new List<Book>();

            foreach (XmlNode node in nodes)
            {
                Book book = new Book();
                try
                {
                    if (node.SelectSingleNode("BookData/AuthorsText").InnerText == null)
                    {
                        MessageBox.Show("Could not find this book. Please enter data by hand.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        textBarcode.Clear();
                        return;
                    }
                }
                catch
                {
                    MessageBox.Show("Could not find this book. Please enter data by hand.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 //   textBarcode.Clear();
                    return;

                }
                book.author = node.SelectSingleNode("BookData/AuthorsText").InnerText;
                book.title = node.SelectSingleNode("BookData/Title").InnerText;

                book.ISBN = node.SelectSingleNode("BookData").Attributes["isbn"].Value;

                books.Add(book);
                MessageBox.Show(book.author);
                addInfo(book.author, book.title, book.ISBN);
                textBarcode.Clear();
            }

          //  MessageBox.Show("Total books: " + books.Count);
        }

        private void addInfo(string Author, string Title, string ISBN)
        {
            textAuthor.Text = Author;
            textTitle.Text = Title;
            textISBN.Text = ISBN;

        }
        class Book
        {
            public string ISBN;
            public string title;
            public string author;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
        int time = 10;
        bool cancel = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if(time > 0)
            {
                labelTime.Text = time.ToString();
                button1.Text = "Change Data";
                cancel = true;
                labelTime.Visible = true;
                time--;
            //   MessageBox.Show(time.ToString());
            }
            if(time <= 0)
            {
                cancel = false;
                button1.Text = "Add to List";
                timer1.Stop();
                time = 10;
                labelTime.Visible = false;
                MessageBox.Show("Submitting");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(cancel)
            {
                timer1.Stop();
                labelTime.Visible = false;
                time = 10;
                cancel = false;
                button1.Text = "Add to List";
            }
            else
            {
                timer1.Start();

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
           Thread tcpServer = new Thread(new ParameterizedThreadStart(TCPServerRun));
            //TCPServerRun();
           tcpServer.Start();

        }
        bool on = true;

        TcpListener tcplistener = new TcpListener(IPAddress.Any, 5004);
        private void TCPServerRun(object test)
        {


            try
            {
                MessageBox.Show("Starting Listener");

                tcplistener.Start();
            }
            catch { MessageBox.Show("COULDNT START TPCSERVER"); return; }



            while (on == true)
            {

                try
                {
                    TcpClient client = tcplistener.AcceptTcpClient();

                   Thread tcpHandlerThread = new Thread(new ParameterizedThreadStart(tcpHandler));
                  // tcpHandlerThread.Start(client);

                    tcpHandler(client);
                }
                catch
                {

                    tcplistener.Stop();
                  //  MessageBox.Show("Stopping Listener");


                }


            }



        }

        string bCode = "";
        private void tcpHandler(object client)
        {
            TcpClient mClient = (TcpClient)client;
            NetworkStream stream = mClient.GetStream();
            byte[] message = new byte[1024];

            stream.Read(message, 0, message.Length);
            bCode = Encoding.ASCII.GetString(message);

            stream.Close();
            mClient.Close();
            MessageBox.Show(bCode);
            this.textBarcode.Text = bCode;
            GetXMLBarcodeData();
        }
    }
}

可能的提示:

这可能与我的线程如何工作有关吗?

*编辑:* **我已更新代码,将条形码放入textBox,然后用于获取数据。这似乎不起作用,因为它无法访问除创建它之外的线程上的控件&#39;

1 个答案:

答案 0 :(得分:1)

如果在自动输入失败时手动用户输入成功,最简单的方法就是将自动输入替换为对手动控制BeginInvoke的调用。对于您的代码,这将是:

textBarcode.BeginInvoke(new Action(() => { 
   textBarcode.Text = bCode;
   GetXMLBarcodeData();
}));