| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Text;
- using System.IO;
- using System.Fire.Serialize;
- namespace System.Net.ApiService {
- public class client {
- protected const string DEFAULT_USER_AGENT = "BPClient/1.0";
- #region Json
- public static T Get_Json<T>(string Url, NameValueCollection Headers = null) {
- return Get_Json<T>(Url, Headers, null);
- }
- public static T Get_Json<T>(string Url, NameValueCollection Headers, NameValueCollection query) {
- try {
- HttpWebRequest request = CreateRequest(Url, Headers, query);
- request.Method = "GET";
- WebResponse response = request.GetResponse();
- return Read_Json<T>(response);
- } catch(Exception ex) {
- System.Diagnostics.Debug.WriteLine(GetClientErrorMessage(ex, Url, Headers, query));
- }
- return Read_Json<T>(null);
- }
- public static System.Text.Json.JsonDocument Get_Json(string Url, NameValueCollection Headers = null) {
- return Get_Json(Url, Headers, null);
- }
- public static System.Text.Json.JsonDocument Get_Json(string Url, NameValueCollection Headers, NameValueCollection query) {
- try {
- HttpWebRequest request = CreateRequest(Url, Headers, query);
- request.Method = "GET";
- WebResponse response = request.GetResponse();
- Stream data = response.GetResponseStream();
- StreamReader tr = new StreamReader(data);
- string raw = tr.ReadToEnd();
- return System.Text.Json.JsonDocument.Parse(raw);
- } catch(Exception ex) {
- System.Diagnostics.Debug.WriteLine(GetClientErrorMessage(ex, Url, Headers, query));
- return null;
- }
- }
- public static T Post_Json<T>(string Url, NameValueCollection Headers, NameValueCollection post) {
- return Post_Json<T>(Url, Headers, null, post);
- }
- public static System.Text.Json.JsonDocument Post_Json(string Url, NameValueCollection Headers, NameValueCollection post) {
- return Post_Json(Url, Headers, null, post);
- }
- public static T Post_Json<T>(string Url, NameValueCollection Headers, NameValueCollection query, NameValueCollection post) {
- HttpWebRequest request = CreateRequest(Url, Headers, query);
- string post_string = QueryString(post);
- /*if (post != null && post.Count > 0) {
- foreach (string key in query) {
- post_string += (post_string == "" ? "?" : "&") + UrlEncode(key) + "=" + UrlEncode(query[key]);
- }
- }*/
- byte[] content = System.Text.Encoding.ASCII.GetBytes(post_string);
- request.Method = "POST";
- request.ContentLength = content.Length;
- Stream dataStream = request.GetRequestStream();
- dataStream.Write(content, 0, content.Length);
- dataStream.Close();
- try {
- WebResponse response = request.GetResponse();
- return Read_Json<T>(response);
- } catch(Exception ex) {
- System.Diagnostics.Debug.WriteLine(GetClientErrorMessage(ex, Url, Headers, query, post));
- return Activator.CreateInstance<T>();
- }
- }
- public static System.Text.Json.JsonDocument Post_Json(string Url, NameValueCollection Headers, NameValueCollection query, NameValueCollection post) {
- HttpWebRequest request = CreateRequest(Url, Headers, query);
- string post_string = QueryString(post);
- /*if (post != null && post.Count > 0) {
- foreach (string key in post) {
- post_string += (post_string == "" ? "?" : "&") + UrlEncode(key) + "=" + UrlEncode(post[key]);
- }
- }*/
- byte[] content = System.Text.Encoding.ASCII.GetBytes(post_string);
- request.Method = "POST";
- request.ContentLength = content.Length;
- Stream dataStream = request.GetRequestStream();
- dataStream.Write(content, 0, content.Length);
- dataStream.Close();
- try {
- WebResponse response = request.GetResponse();
- Stream data = response.GetResponseStream();
- StreamReader tr = new StreamReader(data);
- string raw = tr.ReadToEnd();
- return System.Text.Json.JsonDocument.Parse(raw);
- } catch(Exception ex) {
- System.Diagnostics.Debug.WriteLine(GetClientErrorMessage(ex, Url, Headers, query, post));
- return null;
- }
- }
- protected static T Read_Json<T>(WebResponse response) {
- Stream data = response.GetResponseStream();
- StreamReader tr = new StreamReader(data);
- string raw = tr.ReadToEnd();
- return System.Text.Json.JsonSerializer.Deserialize<T>(raw);
- }
- #endregion
- #region Xml
- public static Xml.XmlDocument Get_Xml(string Url, NameValueCollection Headers = null) {
- try {
- HttpWebRequest request = CreateRequest(Url, Headers);
- request.Method = "GET";
- WebResponse response = request.GetResponse();
- return Read_Xml(response);
- } catch(Exception ex) {
- return null;
- }
- }
- public static Xml.XmlDocument Get_Xml(string Url, NameValueCollection Headers, NameValueCollection query) {
- try {
- HttpWebRequest request = CreateRequest(Url, Headers, query);
- request.Method = "GET";
- WebResponse response = request.GetResponse();
- return Read_Xml(response);
- } catch (Exception ex) {
- return null;
- }
- }
- public static Xml.XmlDocument Post_Xml(string Url, NameValueCollection Headers, NameValueCollection post) {
- return Post_Xml(Url, Headers, null, post);
- }
- public static Xml.XmlDocument Post_Xml(string Url, NameValueCollection Headers, NameValueCollection query, NameValueCollection post) {
- HttpWebRequest request = CreateRequest(Url, Headers, query);
- string post_string = QueryString(post);
- /*if (post != null && post.Count > 0) {
- foreach (string key in query) {
- post_string += (post_string == "" ? "?" : "&") + UrlEncode(key) + "=" + UrlEncode(query[key]);
- }
- }*/
- byte[] content = System.Text.Encoding.ASCII.GetBytes(post_string);
- request.Method = "POST";
- request.ContentLength = content.Length;
- Stream dataStream = request.GetRequestStream();
- dataStream.Write(content, 0, content.Length);
- dataStream.Close();
- WebResponse response = request.GetResponse();
- return Read_Xml(response);
- }
- protected static Xml.XmlDocument Read_Xml(WebResponse response) {
- Stream data = response.GetResponseStream();
- StreamReader tr = new StreamReader(data);
- string raw = tr.ReadToEnd();
- Xml.XmlDocument doc = new Xml.XmlDocument();
- doc.LoadXml(raw);
- return doc;
- }
- #endregion
- #region String
- public static string Get_String(string Url, NameValueCollection Headers = null) {
- HttpWebRequest request = CreateRequest(Url, Headers);
- request.Method = "GET";
- try {
- WebResponse response = request.GetResponse();
- return Read_String(response);
- } catch(Exception ex) {
- System.Diagnostics.Debug.WriteLine(GetClientErrorMessage(ex, Url, Headers, null, null));
- return GetClientErrorMessage(ex);
- }
- }
- public static string Get_String(string Url, NameValueCollection Headers, NameValueCollection query) {
- HttpWebRequest request = CreateRequest(Url, Headers, query);
- request.Method = "GET";
- try {
- WebResponse response = request.GetResponse();
- return Read_String(response);
- } catch(Exception ex) {
- System.Diagnostics.Debug.WriteLine(GetClientErrorMessage(ex, Url, Headers, query, null));
- return GetClientErrorMessage(ex, Url, Headers, query);
- }
- }
- public static string Post_String(string Url, NameValueCollection Headers, NameValueCollection post) {
- return Post_String(Url, Headers, null, post);
- }
- public static string Post_String(string Url, NameValueCollection Headers, NameValueCollection query, NameValueCollection post) {
- try {
- HttpWebRequest request = CreateRequest(Url, Headers, query);
- string post_string = QueryString(post);
- /*if (post != null && post.Count > 0) {
- foreach (string key in query) {
- post_string += (post_string == "" ? "?" : "&") + UrlEncode(key) + "=" + UrlEncode(query[key]);
- }
- }*/
- byte[] content = System.Text.Encoding.ASCII.GetBytes(post_string);
- request.Method = "POST";
- request.ContentLength = content.Length;
- Stream dataStream = request.GetRequestStream();
- dataStream.Write(content, 0, content.Length);
- dataStream.Close();
- WebResponse response = request.GetResponse();
- return Read_String(response);
- } catch(Exception ex) {
- System.Diagnostics.Debug.WriteLine(GetClientErrorMessage(ex, Url, Headers, query, post));
- return GetClientErrorMessage(ex, Url, Headers, query, post);
- }
- }
- protected static string Read_String(WebResponse response) {
- try {
- Stream data = response.GetResponseStream();
- StreamReader tr = new StreamReader(data);
- string raw = tr.ReadToEnd();
- return raw;
- } catch(Exception ex) {
- return GetClientErrorMessage(ex);
- }
- }
- #endregion
- #region Object
- public static T Get_Object<T>(string Url, NameValueCollection Headers = null) {
- string s = Get_String(Url, Headers);
- byte[] b = Convert.FromBase64String(s);
- MemoryStream mem = new MemoryStream(b);
- return Serializer.DeserializeObject<T>(mem);
- }
- public static T Get_Object<T>(string Url, NameValueCollection Headers, NameValueCollection query) {
- string s = Get_String(Url, Headers, query);
- byte[] b = Convert.FromBase64String(s);
- MemoryStream mem = new MemoryStream(b);
- return Serializer.DeserializeObject<T>(mem);
- }
- public static T Post_Object<T>(string Url, NameValueCollection Headers, NameValueCollection post) {
- string s = Post_String(Url, Headers, post);
- byte[] b = Convert.FromBase64String(s);
- MemoryStream mem = new MemoryStream(b);
- return Serializer.DeserializeObject<T>(mem);
- }
- public static T Post_Object<T>(string Url, NameValueCollection Headers, NameValueCollection post, NameValueCollection query) {
- string s = Post_String(Url, Headers, query, post);
- byte[] b = Convert.FromBase64String(s.Trim());
- MemoryStream mem = new MemoryStream(b);
- return Serializer.DeserializeObject<T>(mem);
- }
- #endregion
- protected static string GetClientErrorMessage() {
- return "Failed to connect";
- }
- protected static string GetClientErrorMessage(Exception ex, string Url, NameValueCollection Headers) {
- return "Url: "+ Url + "\n" + ex.Message + "\n" + ex.Source + "\n" + (ex.TargetSite == null ? "" : GetMethodName(ex.TargetSite) + "\n") + ex.StackTrace + (ex.InnerException != null ? "\n" + GetClientErrorMessage(ex.InnerException, 1) : "");
- }
- protected static string GetClientErrorMessage(Exception ex, string Url, NameValueCollection Headers, NameValueCollection query) {
- return "Url: " + Url + "\nQuery: " + QueryString(query) + "\n" + ex.Message + "\n" + ex.Source + "\n" + (ex.TargetSite == null ? "" : GetMethodName(ex.TargetSite) + "\n") + ex.StackTrace + (ex.InnerException != null ? "\n" + GetClientErrorMessage(ex.InnerException, 1) : "");
- }
- protected static string GetClientErrorMessage(Exception ex, string Url, NameValueCollection Headers, NameValueCollection query, NameValueCollection post) {
- return "Url: " + Url + "\nQuery: " + QueryString(query) + "\nPost: "+ QueryString(post) +"\nMessage: " + ex.Message + "\nSource: " + ex.Source + "\nTarget: " + (ex.TargetSite == null ? "" : GetMethodName(ex.TargetSite) + "\n") + "StackTrace: " + ex.StackTrace + (ex.InnerException != null ? "\n" + GetClientErrorMessage(ex.InnerException, 1) : "");
- }
- protected static string GetClientErrorMessage(Exception ex) {
- return "Message: " + ex.Message +
- "\nSource: " + ex.Source +
- "\nTargetSite: " + (ex.TargetSite == null ? "" : GetMethodName(ex.TargetSite)) +
- "\nStackTrace: " + ex.StackTrace +
- (ex.InnerException!=null?"\n"+GetClientErrorMessage(ex.InnerException,1):"");
- }
- protected static string GetClientErrorMessage(Exception ex, int deep) {
- return (new string(' ', deep * 2)) + "Message: " + ex.Message +
- "\n" + (new string(' ', deep * 2)) + "Source: " + ex.Source +
- "\n" + (new string(' ', deep * 2)) + "TargetSite: " + (ex.TargetSite == null ? "" : GetMethodName(ex.TargetSite)) +
- "\n" + (new string(' ', deep * 2)) + "Stack: " + ex.StackTrace +
- (ex.InnerException != null ? "\n" + GetClientErrorMessage(ex.InnerException, deep+1) : "");
- }
- protected static string GetMethodName(Reflection.MethodBase mi) {
- string res = "";
- if (mi.IsPublic) res += "public ";
- if (mi.IsPrivate) res += "private ";
- if (mi.IsFinal) res += "final ";
- if (mi.IsVirtual) res += "virtual ";
- if (mi.IsStatic) res += "static ";
- if (mi.Module != null)
- res += mi.Module.Name + "::";
- res += mi.Name;
- res += "(";
- Reflection.ParameterInfo[] pis = mi.GetParameters();
- bool bFirst = true;
- foreach (Reflection.ParameterInfo pi in pis) {
- if (bFirst) res += ", ";
- res += GetParameter(pi);
- bFirst = false;
- }
- res += ")";
- return res;
- }
- protected static string GetMethodName(Reflection.MethodInfo mi) {
- string res = "";
- if (mi.IsPublic) res += "public ";
- if (mi.IsPrivate) res += "private ";
- if (mi.IsFinal) res += "final ";
- if (mi.IsVirtual) res += "virtual ";
- if (mi.IsStatic) res += "static ";
- if (mi.ReturnType == null) res += "void ";
- else res += mi.ReturnType.Name+" ";
- if (mi.Module != null)
- res += mi.Module.Name + "::";
- res += mi.Name;
- res += "(";
- Reflection.ParameterInfo[] pis = mi.GetParameters();
- bool bFirst = true;
- foreach(Reflection.ParameterInfo pi in pis) {
- if (bFirst) res += ", ";
- res += GetParameter(pi);
- bFirst = false;
- }
- res += ")";
- return res;
- }
- protected static string GetParameter(Reflection.ParameterInfo pi) {
- string res="";
- res += "(";
- if (pi.IsOut) res += "out ";
- if (pi.IsIn) res += "in ";
- if (pi.IsOptional) res += "optional ";
- if (pi.IsRetval) res += "retval ";
- if (pi.IsLcid) res += "lcid ";
- res += pi.ParameterType.Name;
- res += ")";
- res += pi.Name;
- return res;
- }
- protected static string UrlEncode(string s) {
- return System.Net.WebUtility.UrlEncode(s);
- }
- protected static HttpWebRequest CreateRequest(string url, NameValueCollection Headers) {
- if (url.StartsWith("localhost"))
- url = "127.0.0.1" + url.Substring(9);
- string prefix = "";
- if (url.Contains(":")) {
- string protocol = url.Substring(0, url.IndexOf(':'));
- switch (protocol) {
- case "ftp":
- case "http":
- case "https":
- case "sftp":
- break;
- default:
- prefix = "http://";
- break;
- }
- } else {
- prefix = "http://";
- }
- System.Diagnostics.Debug.WriteLine("callUrl1: " + prefix + url);
- HttpWebRequest request = WebRequest.CreateHttp(prefix + url);
- if (Headers != null && Headers.Count > 0)
- request.Headers.Add(Headers);
- if (DEFAULT_USER_AGENT != "")
- request.Headers.Add(HttpRequestHeader.UserAgent, DEFAULT_USER_AGENT);
- request.Headers.Add(HttpRequestHeader.AcceptEncoding, "br, gzip");
- request.Headers.Add(HttpRequestHeader.Connection, "close");
- return request;
- }
- protected static HttpWebRequest CreateRequest(string url, NameValueCollection Headers, NameValueCollection query) {
- string query_string = QueryString(query);
- /*if (query != null && query.Count > 0) {
- foreach (string key in query) {
- query_string += (query_string == "" ? "?" : "&") + UrlEncode(key) + "=" + UrlEncode(query[key]);
- }
- }*/
- if (url.StartsWith("localhost"))
- url = "127.0.0.1" + url.Substring(9);
- string prefix = "";
- if(url.Contains(":")) {
- string protocol = url.Substring(0, url.IndexOf(':'));
- switch(protocol) {
- case "ftp":
- case "http":
- case "https":
- case "sftp":
- break;
- default:
- prefix = "http://";
- break;
- }
- } else {
- prefix = "http://";
- }
- string callUrl = prefix + url + query_string;
- System.Diagnostics.Debug.WriteLine("callUrl2: " + callUrl);
- HttpWebRequest request = WebRequest.CreateHttp(callUrl);
- if(Headers!=null && Headers.Count>0)
- request.Headers.Add(Headers);
- if (DEFAULT_USER_AGENT != "")
- request.Headers.Add(HttpRequestHeader.UserAgent, DEFAULT_USER_AGENT);
- request.Headers.Add(HttpRequestHeader.AcceptEncoding, "br, gzip");
- request.Headers.Add(HttpRequestHeader.Connection, "close");
- return request;
- }
- protected static string QueryString(NameValueCollection query) {
- string res = "";
- if (query == null || query.Count==0)
- return "";
- foreach (string key in query.AllKeys) {
- res += (res.Length==0 ? "?" : "&") + UrlEncode(key) + "=" + UrlEncode(query[key]);
- }
- return res;
- }
- protected static string QueryString(Dictionary<string,string> query) {
- string res = "";
- if (query == null || query.Count == 0)
- return "";
- foreach (KeyValuePair<string,string> itm in query) {
- res += (res.Length == 0 ? "?" : "&") + UrlEncode(itm.Key) + "=" + UrlEncode(itm.Value);
- }
- return res;
- }
- protected static string QueryString(Dictionary<string, object> query) {
- string res = "";
- if (query == null || query.Count == 0)
- return "";
- foreach (KeyValuePair<string, object> itm in query) {
- res += (res.Length == 0 ? "?" : "&") + UrlEncode(itm.Key) + "=" + UrlEncode(itm.Value.ToString());
- }
- return res;
- }
- protected static string HeaderString(NameValueCollection Headers) {
- return HeaderString(Headers, 0);
- }
- protected static string HeaderString(NameValueCollection Headers, int deep) {
- string res = "";
- if (Headers == null || Headers.Count == 0)
- return "";
- foreach (string key in Headers) {
- res += (res.Length==0?"":"\r\n") + (new string(' ', deep*2)) + key + ": " + Headers[key];
- }
- return res;
- }
- }
- }
|