Exemplo n.º 1
0
        public string Write(string uri, GenericDocument content)
        {
            HttpResponseMessage response;

            string url        = SetURL(uri);
            Uri    requestUri = new Uri(url);

            var         mediaType = content.GetMimetype();
            HttpContent httpBody  = new StringContent(content.GetContent());

            httpBody.Headers.ContentType = new MediaTypeHeaderValue(content.GetMimetype());
            response = m_httpClient.PutAsync(requestUri, httpBody).Result;

            // either this - or check the status to retrieve more information
            response.EnsureSuccessStatusCode();
            // get the rest/content of the response in a synchronous way
            var result = response.Content.ReadAsStringAsync().Result;

            if (response.IsSuccessStatusCode)
            {
                return(result);
            }
            else
            {
                result = string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                Console.WriteLine(result);
                throw new DocumentIOException(result);
            }
        }
Exemplo n.º 2
0
        public async Task <string> WriteAsync(string uri, GenericDocument content)
        {
            string url        = SetURL(uri);
            Uri    requestUri = new Uri(url);

            HttpContent httpBody = new StringContent(content.GetContent());

            httpBody.Headers.ContentType = new MediaTypeHeaderValue(content.GetMimetype());

            using (var r = await m_httpClient.PutAsync(requestUri, httpBody))
            {
                string result = await r.Content.ReadAsStringAsync();

                return(result);
            }
        }
Exemplo n.º 3
0
        /*
         * Accept a Document ID (URI) from the console. Read the desired
         * document from the Documents database in MarkLogic. Display the
         * returned document plus write the document to the current working
         * directory.
         */
        static void ReadFromDatabase(DatabaseClient dbClient)
        {
            // Get the desired document's URI from the console.
            Console.Write("Document URI: ");
            var uri = Console.ReadLine();

            // The document content will be saved to the filename specified.
            //  If not filename is specified, the filename part of the URI
            //  is used as the filename.

            string myDocumentPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string defaultFname   = string.Empty;

            if (uri.StartsWith("/") || uri.StartsWith("\\"))
            {
                defaultFname = myDocumentPath + uri;
            }
            else
            {
                defaultFname = myDocumentPath + "/" + uri;
            }
            defaultFname = defaultFname.Replace("\\", "/");
            Console.Write("Save as (ENTER for " + defaultFname + "): ");
            string filename = Console.ReadLine();

            if (filename.Length == 0)
            {
                filename = defaultFname;
            }

            // Create a DocumentManager to act as our interface for
            //  reading and writing to/from the database.
            DocumentManager mgr = dbClient.NewDocumentManager();

            // Use the DocumentManager object to read a document
            // with the uri of "/doc1.xml" from the "Documents" database.
            string mimetype = string.Empty;
            string content  = string.Empty;

            // Use the GenericDocument class to read a non-binary document
            //  from the "Documents" database. This example code reads text,
            //  JSON and XML data but binary is not implemented.
            GenericDocument doc = mgr.Read(uri);

            mimetype = doc.GetMimetype();
            content  = doc.GetContent();

            // Display the retuned document's mime type and content.
            Console.WriteLine(" ");
            Console.WriteLine("---Document Saved-------------");
            Console.WriteLine("---Document Content from DB---");
            Console.Write("Mime type: ");
            Console.WriteLine(mimetype);
            Console.Write("Content: ");
            Console.WriteLine(content);

            // Write the returned content to the specified file.
            //File.WriteAllText(filename, content);
            FileInfo file = new FileInfo(filename);

            file.Directory.Create();             // If the directory already exists, this method does nothing.
            File.WriteAllText(file.FullName, content);
        }