ソースを参照

添加Http请求工具

yuzhengyang 9 年 前
コミット
84ce260c97

+ 52 - 0
Fork.Net/Y.Utils.Net20/HttpUtils/HttpTool.cs

@@ -0,0 +1,52 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Net;
+using System.Text;
+using Y.Utils.Net20.StringUtils;
+
+namespace Y.Utils.Net20.HttpUtils
+{
+    public class HttpTool
+    {
+        public static string Get(string url, string encoding = "utf-8")
+        {
+            string result = "";
+            Encoding myEncoding = Encoding.GetEncoding(encoding);
+            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
+            req.Method = "GET";
+            try
+            {
+                using (WebResponse wr = req.GetResponse())
+                {
+                    //在这里对接收到的页面内容进行处理
+                    result = new StreamReader(wr.GetResponseStream(), myEncoding).ReadToEnd();
+                }
+            }
+            catch (Exception e) { }
+            return result;
+        }
+        public static T Get<T>(string url, string encoding = "utf-8")
+        {
+            Encoding myEncoding = Encoding.GetEncoding(encoding);
+            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
+            req.Method = "GET";
+            try
+            {
+                using (WebResponse wr = req.GetResponse())
+                {
+                    //在这里对接收到的页面内容进行处理
+                    string response = new StreamReader(wr.GetResponseStream(), myEncoding).ReadToEnd();
+                    if (!StringTool.IsNullOrWhiteSpace(response))
+                    {
+                        T result = JsonConvert.DeserializeObject<T>(response);
+                        return result;
+                    }
+                }
+            }
+            catch (Exception e) { }
+            return default(T);
+        }
+    }
+}

+ 4 - 1
Fork.Net/Y.Utils.Net20/MysqlUtils/MysqlTool.cs

@@ -104,9 +104,12 @@ namespace Y.Utils.Net20.MysqlUtils
             return dataset.Tables[0];
         }
         //Test
+        //string constring = "Server=db4free.net;Database=db4free_yzy; User=yuzhengyang;Password=yzy50665;Use Procedure Bodies=false;Charset=utf8;Allow Zero Datetime=True; Pooling=false; Max Pool Size=50;";
+        //MysqlTool ms = new MysqlTool(connstr: constring);
+        //ms.ExecuteDataTable2("select * from user");
         public void ExecuteDataTable2(string sql, params MySqlParameter[] parameters)
         {
-            
+
 
             MySqlConnection sqlCon = new MySqlConnection(ConnectionString);
             //设置查询命令

+ 19 - 0
Fork.Net/Y.Utils.Net20/StringUtils/StringTool.cs

@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Y.Utils.Net20.StringUtils
+{
+    public class StringTool
+    {
+        public static bool IsNullOrWhiteSpace(string str)
+        {
+            if (str == null)
+                return true;
+            if (str.Trim().Length == 0)
+                return true;
+
+            return false;
+        }
+    }
+}

+ 9 - 1
Fork.Net/Y.Utils.Net20/Y.Utils.Net20.csproj

@@ -34,6 +34,10 @@
       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\Dlls\MySql\v2.0\MySql.Data.dll</HintPath>
     </Reference>
+    <Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
+      <HintPath>..\..\..\..\GitHub\Marx\Marx\packages\Newtonsoft.Json.9.0.1\lib\net20\Newtonsoft.Json.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.configuration" />
     <Reference Include="System.Data" />
@@ -47,11 +51,13 @@
     <Compile Include="FileUtils\FileCode.cs" />
     <Compile Include="FileUtils\FileTool.cs" />
     <Compile Include="HookUtils\UserActivityHook.cs" />
+    <Compile Include="HttpUtils\HttpTool.cs" />
     <Compile Include="ListUtils\ListTool.cs" />
     <Compile Include="LogUtils\Log.cs" />
     <Compile Include="MysqlUtils\MysqlTool.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="StringUtils\SimilarString.cs" />
+    <Compile Include="StringUtils\StringTool.cs" />
     <Compile Include="TimeUtils\DateTimeConvert.cs" />
     <Compile Include="TimeUtils\DateTimeTool.cs" />
     <Compile Include="TxtUtils\IniTool.cs" />
@@ -59,7 +65,9 @@
     <Compile Include="WindowsAPI\ComputerAFK.cs" />
     <Compile Include="WindowsAPI\WindowInfo.cs" />
   </ItemGroup>
-  <ItemGroup />
+  <ItemGroup>
+    <None Include="packages.config" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

+ 4 - 0
Fork.Net/Y.Utils.Net20/packages.config

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net20" />
+</packages>