Beispiel #1
0
        public void Request(SimpleRESTForm form, OnRESTResponse onRequest)
        {
            m_request             = (HttpWebRequest)WebRequest.Create(url);
            m_request.ContentType = form.header[1];
            m_request.Method      = "POST";
            m_request.Timeout     = defaultTimeOut;
            m_request.KeepAlive   = true;
            m_request.Credentials = CredentialCache.DefaultCredentials;

            Stream reqStream = m_request.GetRequestStream();

            //FileStream reqStream = new FileStream("Dump.txt", FileMode.OpenOrCreate);
            form._writeStream(reqStream);
            reqStream.Close();
            try
            {
                m_response = m_request.GetResponse();
                var strm = m_response.GetResponseStream();
                m_stream = new StreamReader(strm);
            }
            catch (WebException e)
            {
                m_error = e;
                var stream = e.Response.GetResponseStream();
                m_stream = new StreamReader(stream);
            }
            finally
            {
                m_request = null;
            }
            onRequest?.Invoke(m_error == null, m_error == null ? null : m_error.ToString(), m_stream);
        }
Beispiel #2
0
        public static SimpleRESTForm FromObject <T>(T obj) where T : new()
        {
            var result    = new SimpleRESTForm();
            var objType   = obj.GetType();
            var objFields = objType.GetFields();

            foreach (var field in objFields)
            {
                var fieldValue = field.GetValue(obj);
                result.AddField(field.Name, fieldValue == null?"":fieldValue.ToString());
            }
            return(result);
        }
Beispiel #3
0
 public void RequestAsync(SimpleRESTForm form, OnRESTResponse onRequest)
 {
     Worker.WorkerDelegate onWork = delegate(out object result)
     {
         Request(form, onRequest);
         result = null;
         return(true);
     };
     Worker.WorkerFinishedDelegate onFinish = delegate(Exception e, object result)
     {
         //if (e != null)
         //    throw e;
         //onRequest?.Invoke(m_error == null, m_error == null ? null : m_error.ToString(), m_stream);
         onRequest?.Invoke(m_error == null, m_error == null ? null : m_error.ToString(), m_stream);
     };
     Worker.StartWorker(onWork, onFinish);
 }