public static string Post(string _URL, object _DATA, TConnector.ConnectorOnEventDelegate _onresponse = null, TConnector.ConnectorOnEventDelegate _onerror = null) { HttpWebRequest http = (HttpWebRequest)WebRequest.Create(_URL); if (Config.ProxyAuth.URL != "") { http.Proxy = new WebProxy("http://" + Config.ProxyAuth.URL + (Config.ProxyAuth.Port != 0 ? (":" + Config.ProxyAuth.Port + "/") : "/")); http.Proxy.Credentials = new NetworkCredential(Config.ProxyAuth.Login, Config.ProxyAuth.Password); } else { http.UseDefaultCredentials = true; http.Proxy = WebRequest.GetSystemWebProxy(); } http.Method = "POST"; byte[] bytes; if (_DATA.ToString().StartsWith("<")) { http.ContentType = "text/xml"; bytes = System.Text.Encoding.UTF8.GetBytes(_DATA.ToString()); } else { http.ContentType = "application/x-www-form-urlencoded"; bytes = System.Text.Encoding.UTF8.GetBytes(_DATA.ToString()); } Stream strIn = http.GetRequestStream(); strIn.Write(bytes, 0, bytes.Length); strIn.Close(); WebResponse ret = http.GetResponse(); Stream strOut = ret.GetResponseStream(); StreamReader reader = new StreamReader(strOut); return reader.ReadToEnd(); }
public static void PostAsync(string _URL, object _DATA, TConnector.ConnectorOnEventDelegate _onresponse = null, TConnector.ConnectorOnEventDelegate _onerror = null) { var client = new HttpClient(); byte[] bytes; bytes = System.Text.Encoding.ASCII.GetBytes(_DATA.ToString()); HttpContent content = new ByteArrayContent(bytes); client.PostAsync(_URL, content).ContinueWith( (postTask) => { object retval; HttpResponseMessage msg = new HttpResponseMessage(); try { msg = postTask.Result; msg.EnsureSuccessStatusCode(); } catch (Exception ex) { if (_onerror != null) _onerror(ex); return; } Task<Stream> objResponseStreamTask = msg.Content.ReadAsStreamAsync(); Stream objResponseStream = objResponseStreamTask.Result; XmlDocument xmldoc = new XmlDocument(); try { XmlTextReader objXMLReader = new XmlTextReader(objResponseStream); xmldoc.Load(objXMLReader); objXMLReader.Close(); retval = xmldoc; } catch (Exception) { objResponseStream.Position = 0; StreamReader sr = new StreamReader(objResponseStream); retval = sr.ReadToEnd(); } if (_onresponse != null) _onresponse(retval); }); }
public static void PostXMLAsync(string _URL, XmlDocument _DATA, TConnector.ConnectorOnEventDelegate _onresponse = null, TConnector.ConnectorOnEventDelegate _onerror = null) { PostAsync(_URL, _DATA.InnerXml, _onresponse, _onerror); }
public static string PostXML(string _URL, XmlDocument _DATA, TConnector.ConnectorOnEventDelegate _onresponse = null, TConnector.ConnectorOnEventDelegate _onerror = null) { return Post(_URL, _DATA.InnerXml, _onresponse, _onerror); }