Esempio n. 1
0
        public bool Import(ScanFile scanFile)
        {
            //#if DEBUG
            //            return true;
            //#endif
            try
            {
                //Import URL ObalkyKnih.cz
                string importUrl = String.Format("https://{0}", OBALKY_KNIH_IMPORT);
                string custom = String.Format("{0}-{1}", this.Sigla, scanFile.Book.SysNo);

                HttpWebRequest requestToServer = (HttpWebRequest)WebRequest.Create(importUrl);
                requestToServer.ContentType = String.Format("multipart/form-data; boundary={0}", PostData.Boundary);
                requestToServer.Method = WebRequestMethods.Http.Post;
                requestToServer.AllowWriteStreamBuffering = false;
                requestToServer.KeepAlive = false;
                requestToServer.Timeout = 60000;

                // Text parameters
                PostData pData = new PostData();
                pData.AddDataParam("login", this.Login);
                pData.AddDataParam("password", this.Password);
                pData.AddDataParam("isbn", scanFile.Book.ISBN);
                pData.AddDataParam("issn", scanFile.Book.ISSN);
                pData.AddDataParam("oclc", scanFile.Book.OCLC);
                pData.AddDataParam("nbn", (String.IsNullOrEmpty(scanFile.Book.NBN) ? custom : scanFile.Book.NBN));
                pData.AddDataParam("author", scanFile.Book.Author);
                pData.AddDataParam("title", scanFile.Book.Title);
                pData.AddDataParam("year", scanFile.Book.Year);
                pData.AddDataParam("ocr", "no");

                // Meta parameters
                string metaXml = GetMetaXmlData(scanFile.PartOfBook, scanFile.PageCount);
                pData.AddFileParam("meta", "meta.xml", metaXml, PostDataContentType.XML);

                // File data
                //string filepath = scanFile.GetScanFilePath();
                //string extension = Path.GetExtension(filepath);
                switch (scanFile.PartOfBook)
                {
                    case PartOfBook.FrontCover:
                        pData.AddFileParam("cover", "cover.jpg", scanFile.GetScanFilePath(), PostDataContentType.JPG);
                        break;
                    case PartOfBook.TableOfContents:
                        pData.AddFileParam("toc", "toc.tif", scanFile.GetScanFilePath(), PostDataContentType.TIFF);
                        break;
                    default:
                        break;
                }

                // Write the http request body directly to the server
                byte[] buffer = pData.GetPostDataBytes();
                requestToServer.ContentLength = buffer.Length;

                using (Stream s = requestToServer.GetRequestStream())
                {
                    s.Write(buffer, 0, buffer.Length);
                    s.Flush();
                    s.Close();
                }

                // Grab the response from the server
                WebResponse response = requestToServer.GetResponse();
                StreamReader responseReader = new StreamReader(response.GetResponseStream());

                //return Encoding.UTF8.GetString(buffer);
                return responseReader.ReadToEnd().Equals("OK", StringComparison.OrdinalIgnoreCase);
            }
            catch (WebException we)
            {
                string message = String.Empty;

                if (we.Response != null)
                {
                    HttpWebResponse response = (we.Response as HttpWebResponse);

                    //WebException will be thrown when a HTTP OK status is not returned
                    switch (response.StatusCode)
                    {
                        case HttpStatusCode.Unauthorized:
                            message = "Chyba autorizace: Přihlašovací údaje nejsou správné.";
                            break;
                        case HttpStatusCode.InternalServerError:
                            message = String.Format("Chyba na straně serveru: {0}", response.StatusDescription);
                            break;
                        default:
                            message = String.Format("{0}: {1}", response.StatusCode, response.StatusDescription);
                            break;
                    }
                }
                else
                {
                    message = String.Format("{0}: {1}", we.Status, we.Message);
                }

                throw new PostDataException(String.Format("Odesílání neúspěšné: {0}", message), we);
            }
            catch (Exception ex)
            {
                throw new PostDataException(String.Format("Počas odesílání nastala neznámá výjimka, je možné, že data nebyla odeslána: {0}", ex.Message), ex);
            }
        }