예제 #1
0
        public static Stream BackendConnect <T>(T model, string urlPath)
        {
            MemoryStream ms     = new MemoryStream();
            XmlWriter    writer = XmlWriter.Create(ms);

            DataContractSerializer serializer = new DataContractSerializer(typeof(T));

            serializer.WriteObject(writer, model);
            Console.WriteLine(serializer.ToString());
            writer.Close();

            byte[] data = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(ms.ToArray()));

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create($"http://fineoicar.azurewebsites.net/{urlPath}");

            req.Method      = "POST";
            req.Accept      = "application/xml";
            req.ContentType = "application/xml";

            Stream reqStream = req.GetRequestStream();

            reqStream.Write(data, 0, data.Length);

            HttpWebResponse resp       = (HttpWebResponse)req.GetResponse();
            Stream          respStream = resp.GetResponseStream();

            return(respStream);
        }
예제 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting ClassFormatter...");

            Class1 class1 = new Class1("Alvaro");
            Class2 class2 = new Class2();

            class2.Values = new List <string> {
                "Alvaro"
            };

            // To serialize the hashtable and its key/value pairs,
            // you must first open a stream for writing.
            // In this case, use a file stream.
            FileStream fs = new FileStream("DataFile.dat", FileMode.Create);

            Console.WriteLine("Starting BinaryFormatter serialization");
            // Construct a BinaryFormatter and use it to serialize the data to the stream.
            BinaryFormatter formatter = new BinaryFormatter();

            try
            {
                formatter.Serialize(fs, class1);
                formatter.Serialize(fs, class2);
            }
            catch (SerializationException e)
            {
                // To serialize a class with BinaryFormatter, we must use the attribute [Serializable] for the class
                Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
                Console.WriteLine("Ended BinaryFormatter serialization");
            }

            Console.WriteLine("Starting DataContract serialization");

            MemoryStream stream1 = new MemoryStream();
            //Serialize the Record object to a memory stream using DataContractSerializer.
            DataContractSerializer serializer = new DataContractSerializer(typeof(Class2));

            serializer.WriteObject(stream1, class2);
            serializer.ToString();

            Console.WriteLine("Ended DataContract serialization");

            Console.ReadLine();
        }