// return:
        //      -1  出错
        //      >=0 命中的记录数
        int SearchLineAmazon(RegisterLine line,
            AccountInfo account,
            out string strError)
        {
            strError = "";

            // ??? this._currentAccount = account;

            line.BiblioSummary = "正在针对 " + account.ServerName + " \r\n检索 " + line.BiblioBarcode + " ...";

            AmazonSearch search = new AmazonSearch();
            // search.MainForm = this.MainForm;
            search.TempFileDir = this.MainForm.UserTempDir;

            // 多行检索中的一行检索
            int nRedoCount = 0;
        REDO:
            int nRet = search.Search(
                account.ServerUrl,
                line.BiblioBarcode.Replace("-", ""),
                "ISBN",
                "[default]",
                true,
                out strError);
            if (nRet == -1)
            {
                if (search.Exception != null && search.Exception is WebException)
                {
                    WebException e = search.Exception as WebException;
                    if (e.Status == WebExceptionStatus.ProtocolError)
                    {
                        // 重做
                        if (nRedoCount < 2)
                        {
                            nRedoCount++;
                            Thread.Sleep(1000);
                            goto REDO;
                        }

#if NO
                        // 询问是否重做
                        DialogResult result = MessageBox.Show(this,
"检索 '" + strLine + "' 时发生错误:\r\n\r\n" + strError + "\r\n\r\n是否重试?\r\n\r\n(Yes: 重试; No: 跳过这一行继续检索后面的行; Cancel: 中断整个检索操作",
"AmazonSearchForm",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1);
                        if (result == System.Windows.Forms.DialogResult.Retry)
                        {
                            Thread.Sleep(1000);
                            goto REDO;
                        }
                        if (result == System.Windows.Forms.DialogResult.Cancel)
                            return -1;
                        goto CONTINUE;
#endif
                        goto ERROR1;
                    }
                }
                goto ERROR1;
            }


            nRet = search.LoadBrowseLines(appendBrowseLine,
                line,
                false,
                out strError);
            if (nRet == -1)
                goto ERROR1;
            return nRet;

            ERROR1:
            strError = "针对服务器 '" + account.ServerName + "' 检索出错: " + strError;
            line.AddBiblioBrowseLine(strError, BiblioRegisterControl.TYPE_ERROR);
            return -1;
        }
        // return:
        //      -1  出错
        //      >=0 命中的记录数
        int SearchLineAmazon(
            string strQueryWord,
            string strFrom,
            AccountInfo account,
            bool bAutoSetFocus,
            out string strError)
        {
            strError = "";

            if (strFrom == "书名" || strFrom == "题名")
                strFrom = "title";
            else if (strFrom == "作者" || strFrom == "著者" || strFrom == "责任者")
                strFrom = "author";
            else if (strFrom == "出版社" || strFrom == "出版者")
                strFrom = "publisher";
            else if (strFrom == "出版日期")
                strFrom = "pubdate";
            else if (strFrom == "主题词")
                strFrom = "subject";
            else if (strFrom == "关键词")
                strFrom = "keywords";
            else if (strFrom == "语言")
                strFrom = "language";
            else if (strFrom == "装订")
                strFrom = "binding";

/* 还可以使用:
            "ISBN",
            "EISBN",
            "ASIN"
*/

            this.ShowMessage("正在针对 " + account.ServerName + " \r\n检索 " + strQueryWord + " ...",
                "progress", false);

            AmazonSearch search = new AmazonSearch();
            // search.MainForm = this.MainForm;
            search.TempFileDir = this.MainForm.UserTempDir;

            search.Timeout = 5 * 1000;
            search.Idle += search_Idle;
            try
            {

                // 多行检索中的一行检索
                int nRedoCount = 0;
            REDO:
                int nRet = search.Search(
                    account.ServerUrl,
                    strQueryWord.Replace("-", ""),
                    strFrom,    // "ISBN",
                    "[default]",
                    true,
                    out strError);
                if (nRet == -1)
                {
                    if (search.Exception != null && search.Exception is WebException)
                    {
                        WebException e = search.Exception as WebException;
                        if (e.Status == WebExceptionStatus.ProtocolError)
                        {
                            // 重做
                            if (nRedoCount < 2)
                            {
                                nRedoCount++;
                                Thread.Sleep(1000);
                                goto REDO;
                            }

#if NO
                        // 询问是否重做
                        DialogResult result = MessageBox.Show(this,
"检索 '" + strLine + "' 时发生错误:\r\n\r\n" + strError + "\r\n\r\n是否重试?\r\n\r\n(Yes: 重试; No: 跳过这一行继续检索后面的行; Cancel: 中断整个检索操作",
"AmazonSearchForm",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1);
                        if (result == System.Windows.Forms.DialogResult.Retry)
                        {
                            Thread.Sleep(1000);
                            goto REDO;
                        }
                        if (result == System.Windows.Forms.DialogResult.Cancel)
                            return -1;
                        goto CONTINUE;
#endif
                            goto ERROR1;
                        }
                    }
                    goto ERROR1;
                }

                nRet = search.LoadBrowseLines(appendBrowseLine,
                    null,   // line,
                    bAutoSetFocus,
                    out strError);
                if (nRet == -1)
                    goto ERROR1;
                return nRet;
            }
            finally
            {
                search.Idle -= search_Idle;
                this.ClearMessage();
            }

        ERROR1:
            strError = "针对服务器 '" + account.ServerName + "' 检索出错: " + strError;
            AddBiblioBrowseLine(strError, TYPE_ERROR, bAutoSetFocus);
            return -1;
        }
Exemple #3
0
        /// <summary>
        /// 检索
        /// </summary>
        /// <param name="strError">返回出错信息</param>
        /// <returns>-1表示出错,其他命中记录的数目</returns>
        public int DoSearch(out string strError)
        {
            strError = "";

            this.ClearList();

            // line.BiblioSummary = "正在针对 " + account.ServerName + " \r\n检索 " + line.BiblioBarcode + " ...";

            this.button_stop.Enabled = true;
            this.button_search.Enabled = false;
            try
            {
                string strFrom = this.GetFromRight();

#if NO
                if (_search != null)
                    _search.Dispose();

                _search = new AmazonSearch();
                // search.MainForm = this.MainForm;
                _search.TempFileDir = this.TempFileDir;
                _search.Idle -= new EventHandler(search_Idle);
                _search.Idle += new EventHandler(search_Idle);
#endif
                if (_search == null)
                {
                    _search = new AmazonSearch();
                    _search.TempFileDir = this.TempFileDir;
                    _search.Idle -= new EventHandler(search_Idle);
                    _search.Idle += new EventHandler(search_Idle);
                }


                // 多行检索中的一行检索
                int nRedoCount = 0;
            REDO:
                int nRet = _search.Search(
                    this.ServerUrl,
                    this.textBox_queryWord.Text.Replace("-", ""),
                    strFrom,    // "ISBN",
                    "[default]",
                    true,
                    out strError);
                if (nRet == -1)
                {
                    if (_search.Exception != null && _search.Exception is WebException)
                    {
                        WebException e1 = _search.Exception as WebException;
                        if (e1.Status == WebExceptionStatus.ProtocolError)
                        {
                            // 重做
                            if (nRedoCount < this.RetryCount)
                            {
                                nRedoCount++;
                                _search.Delay();
                                goto REDO;
                            }

#if NO
                        // 询问是否重做
                        DialogResult result = MessageBox.Show(this,
"检索 '" + strLine + "' 时发生错误:\r\n\r\n" + strError + "\r\n\r\n是否重试?\r\n\r\n(Yes: 重试; No: 跳过这一行继续检索后面的行; Cancel: 中断整个检索操作",
"AmazonSearchForm",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1);
                        if (result == System.Windows.Forms.DialogResult.Retry)
                        {
                                _search.Delay();
                            goto REDO;
                        }
                        if (result == System.Windows.Forms.DialogResult.Cancel)
                            return -1;
                        goto CONTINUE;
#endif
                            strError = "nRedoCount [" + nRedoCount.ToString() + "] " + strError;
                            goto ERROR1;
                        }
                    }
                    goto ERROR1;
                }

                nRet = _search.LoadBrowseLines(appendBrowseLine,
                    null,
                    false,
                    out strError);
                if (nRet == -1)
                    goto ERROR1;

                // 如果有命中的,自动选定第一行
                if (dpTable_items.Rows.Count > 0)
                    dpTable_items.Rows[0].Select(SelectAction.On);

                if (_search.HasNextBatch() == true)
                {
                    AddNextCmdLine();
                }
            }
            finally
            {
                this.button_stop.Enabled = false;
                this.button_search.Enabled = true;
            }

            return _search.HitCount;
        ERROR1:
            strError = "针对服务器 '" + this.ServerUrl + "' 检索出错: " + strError;
            return -1;
        }