Thursday, April 16, 2015

How to send an xml from a c# desktop application to a php server script and parse it?

c#

public partial class frmSending : Form
    {
        public frmSending(string url, string file, XmlDocument xml)
        {
            InitializeComponent();

            this.Url = url;
            this.XmlFile = file;
            this.Xml = xml;
            this.Sent += frmSending_Sent;
        }

        void frmSending_Sent(object sender, EventArgs e)
        {
            if (this.Ex != null)
            {
                MessageBox.Show(Ex.Message, "Алдаа", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                MessageBox.Show("Амжилттай илгээлээ!", "Мэдэгдэл", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.Close();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            thread = new Thread(post_to_internet);
            thread.Start();
        }

        private void post_to_internet()
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.ContentType = "application/xml";
                request.Accept = "application/xml";

                StringWriter stringWriter = new StringWriter();
                XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
                Xml.WriteTo(xmlTextWriter);
                byte[] bytes = Encoding.UTF8.GetBytes(stringWriter.ToString());

                request.ContentLength = bytes.Length;

                using (Stream putStream = request.GetRequestStream())
                {
                    putStream.Write(bytes, 0, bytes.Length);
                }

                // Log the response from Redmine RESTful service
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    this.Invoke(Sent, new object[] { reader.ReadLine(), EventArgs.Empty });
                }

                //using (WebClient request = new WebClient())
                //{
                //    request.UploadFile(Url, "POST", XmlFile);
                //}
            }
            catch (Exception ex)
            {
                this.Ex = ex;
                this.Invoke(Sent, new object[] { ex, EventArgs.Empty });
            }
        }

        public string Url { get; set; }
        public string XmlFile { get; set; }
        public XmlDocument Xml { get; set; }
        private Thread thread { get; set; }
        private event EventHandler Sent;
        public Exception Ex { get; set; }

        private void frmSending_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.F1)
            {
                thread.Abort();
            }
        }
    }
<?php

include "db.config.php";
debug_log('uploaded ' . $fp);
$fp = fopen('php://input', 'rb');
file_put_contents('report.xml', $fp);

//$upload = isset($_FILES) ? reset($_FILES) : null;
//debug_log('uploaded ' . $upload['tmp_name']);
//move_uploaded_file($upload['tmp_name'], $upload['name']);
afterwards the file test.dat on the server contains
<?xml version="1.0" encoding="utf-8"?>
<Foo>
  <bar>
    <type>System.String</type>
    <value>Stackoverflow</value>
  </bar>
  <bar>
    <type>System.Boolean</type>
    <value>True</value>
  </bar>
  <bar>
    <type>System.Char</type>
    <value>x</value>
  </bar>
  <bar>
    <type>System.Int32</type>
    <value>42</value>
  </bar>
</Foo>

No comments: