ソースを参照

修改Http等工具类

yuzhengyang 9 年 前
コミット
1194ff837b

+ 146 - 1
Fork.Net/Y.Utils.Net20/HttpUtils/HttpTool.cs

@@ -4,6 +4,7 @@ using System.Collections.Generic;
 using System.IO;
 using System.IO;
 using System.Net;
 using System.Net;
 using System.Text;
 using System.Text;
+using System.Web;
 using Y.Utils.Net20.StringUtils;
 using Y.Utils.Net20.StringUtils;
 
 
 namespace Y.Utils.Net20.HttpUtils
 namespace Y.Utils.Net20.HttpUtils
@@ -30,7 +31,7 @@ namespace Y.Utils.Net20.HttpUtils
         public static T Get<T>(string url, string encoding = "utf-8")
         public static T Get<T>(string url, string encoding = "utf-8")
         {
         {
             Encoding myEncoding = Encoding.GetEncoding(encoding);
             Encoding myEncoding = Encoding.GetEncoding(encoding);
-            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
+            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
             req.Method = "GET";
             req.Method = "GET";
             try
             try
             {
             {
@@ -48,5 +49,149 @@ namespace Y.Utils.Net20.HttpUtils
             catch (Exception e) { }
             catch (Exception e) { }
             return default(T);
             return default(T);
         }
         }
+        public static string Post(string url, string param, string encoding = "utf-8")
+        {
+            string result = string.Empty;
+            try
+            {
+                Encoding myEncoding = Encoding.GetEncoding(encoding);
+                byte[] byteArray = myEncoding.GetBytes(param); //转化
+                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
+                webReq.Method = "POST";
+                webReq.ContentType = "application/x-www-form-urlencoded";
+                webReq.ContentLength = byteArray.Length;
+                Stream newStream = webReq.GetRequestStream();
+                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
+                newStream.Close();
+                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
+                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
+                result = sr.ReadToEnd();
+                sr.Close();
+                response.Close();
+                newStream.Close();
+            }
+            catch (Exception ex)
+            { }
+            return result;
+        }
+        //public static string PostJson(string url, string param)
+        //{
+        //    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
+        //    request.Method = "POST";
+        //    request.ContentType = "application/json";
+        //    request.ContentLength = Encoding.UTF8.GetByteCount(param);
+        //    Stream myRequestStream = request.GetRequestStream();
+        //    StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
+        //    myStreamWriter.Write(param);
+        //    myStreamWriter.Close();
+        //    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
+        //    Stream myResponseStream = response.GetResponseStream();
+        //    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
+        //    string retString = myStreamReader.ReadToEnd();
+        //    myStreamReader.Close();
+        //    myResponseStream.Close();
+        //    return retString;
+        //}
+        public static string PostJson(string url, string param)
+        {
+            string rs = null;
+            ServicePointManager.DefaultConnectionLimit = 300;
+            System.GC.Collect();
+            CookieContainer cookieContainer = new CookieContainer();
+            // 设置提交的相关参数
+            HttpWebRequest request = null;
+            HttpWebResponse SendSMSResponse = null;
+            Stream dataStream = null;
+            StreamReader SendSMSResponseStream = null;
+            try
+            {
+                request = WebRequest.Create(url) as HttpWebRequest;
+                request.Method = "POST";
+                request.KeepAlive = false;
+                request.ServicePoint.ConnectionLimit = 300;
+                request.AllowAutoRedirect = true;
+                request.Timeout = 10000;
+                request.ReadWriteTimeout = 10000;
+                request.ContentType = "application/json";
+                request.Accept = "application/xml";
+                request.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode("OpenStack"));
+                string strContent = param;
+                byte[] bytes = Encoding.UTF8.GetBytes(strContent);
+                request.Proxy = null;
+                request.CookieContainer = cookieContainer;
+                using (dataStream = request.GetRequestStream())
+                {
+                    dataStream.Write(bytes, 0, bytes.Length);
+                }
+                SendSMSResponse = (HttpWebResponse)request.GetResponse();
+                if (SendSMSResponse.StatusCode == HttpStatusCode.RequestTimeout)
+                {
+                    if (SendSMSResponse != null)
+                    {
+                        SendSMSResponse.Close();
+                        SendSMSResponse = null;
+                    }
+                    if (request != null)
+                    {
+                        request.Abort();
+                    }
+                    return null;
+                }
+                SendSMSResponseStream = new StreamReader(SendSMSResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));
+                string strRespone = SendSMSResponseStream.ReadToEnd();
+
+                return strRespone;
+            }
+            catch (Exception ex)
+            {
+
+                if (dataStream != null)
+                {
+                    dataStream.Close();
+                    dataStream.Dispose();
+                    dataStream = null;
+                }
+                if (SendSMSResponseStream != null)
+                {
+                    SendSMSResponseStream.Close();
+                    SendSMSResponseStream.Dispose();
+                    SendSMSResponseStream = null;
+                }
+                if (SendSMSResponse != null)
+                {
+                    SendSMSResponse.Close();
+                    SendSMSResponse = null;
+                }
+                if (request != null)
+                {
+                    request.Abort();
+                }
+            }
+            finally
+            {
+                if (dataStream != null)
+                {
+                    dataStream.Close();
+                    dataStream.Dispose();
+                    dataStream = null;
+                }
+                if (SendSMSResponseStream != null)
+                {
+                    SendSMSResponseStream.Close();
+                    SendSMSResponseStream.Dispose();
+                    SendSMSResponseStream = null;
+                }
+                if (SendSMSResponse != null)
+                {
+                    SendSMSResponse.Close();
+                    SendSMSResponse = null;
+                }
+                if (request != null)
+                {
+                    request.Abort();
+                }
+            }
+            return rs;
+        }
     }
     }
 }
 }

+ 44 - 0
Fork.Net/Y.Utils.Net20/JsonUtils/JsonTool.cs

@@ -0,0 +1,44 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Y.Utils.Net20.StringUtils;
+using Y.Utils.Net20.TxtUtils;
+
+namespace Y.Utils.Net20.JsonUtils
+{
+    public class JsonTool
+    {
+        public static string ToStr(object value)
+        {
+            return JsonConvert.SerializeObject(value);
+        }
+        public static object ToObjFromStr(string str)
+        {
+            string json = str;
+            if (!StringTool.IsNullOrWhiteSpace(json))
+            {
+                try { return JsonConvert.DeserializeObject(json); } catch (Exception e) { }
+            }
+            return null;
+        }
+        public static T ToObjFromStr<T>(string str)
+        {
+            string json = str;
+            if (!StringTool.IsNullOrWhiteSpace(json))
+            {
+                try { return JsonConvert.DeserializeObject<T>(json); } catch (Exception e) { }
+            }
+            return default(T);
+        }
+        public static T ToObjFromFile<T>(string file)
+        {
+            string json = TxtTool.Read(file);
+            if (!StringTool.IsNullOrWhiteSpace(json))
+            {
+                try { return JsonConvert.DeserializeObject<T>(json); } catch (Exception e) { }
+            }
+            return default(T);
+        }
+    }
+}

+ 5 - 0
Fork.Net/Y.Utils.Net20/TimeUtils/DateTimeConvert.cs

@@ -6,6 +6,11 @@ namespace Y.Utils.Net20.TimeUtils
 {
 {
     public sealed class DateTimeConvert
     public sealed class DateTimeConvert
     {
     {
+        /// <summary>
+        /// yyyy-MM-dd HH:mm:ss
+        /// </summary>
+        /// <param name="dt"></param>
+        /// <returns></returns>
         public static string ToStandardString(DateTime dt)
         public static string ToStandardString(DateTime dt)
         {
         {
             return dt.ToString("yyyy-MM-dd HH:mm:ss");
             return dt.ToString("yyyy-MM-dd HH:mm:ss");

+ 2 - 0
Fork.Net/Y.Utils.Net20/Y.Utils.Net20.csproj

@@ -41,6 +41,7 @@
     <Reference Include="System" />
     <Reference Include="System" />
     <Reference Include="System.configuration" />
     <Reference Include="System.configuration" />
     <Reference Include="System.Data" />
     <Reference Include="System.Data" />
+    <Reference Include="System.Web" />
     <Reference Include="System.Windows.Forms" />
     <Reference Include="System.Windows.Forms" />
     <Reference Include="System.Xml" />
     <Reference Include="System.Xml" />
   </ItemGroup>
   </ItemGroup>
@@ -52,6 +53,7 @@
     <Compile Include="FileUtils\FileTool.cs" />
     <Compile Include="FileUtils\FileTool.cs" />
     <Compile Include="HookUtils\UserActivityHook.cs" />
     <Compile Include="HookUtils\UserActivityHook.cs" />
     <Compile Include="HttpUtils\HttpTool.cs" />
     <Compile Include="HttpUtils\HttpTool.cs" />
+    <Compile Include="JsonUtils\JsonTool.cs" />
     <Compile Include="ListUtils\ListTool.cs" />
     <Compile Include="ListUtils\ListTool.cs" />
     <Compile Include="LogUtils\Log.cs" />
     <Compile Include="LogUtils\Log.cs" />
     <Compile Include="MysqlUtils\MysqlTool.cs" />
     <Compile Include="MysqlUtils\MysqlTool.cs" />