美文网首页
C# 调用网页接口

C# 调用网页接口

作者: Rinaloving | 来源:发表于2019-11-27 15:31 被阅读0次
    代码部分
    using Newtonsoft.Json.Linq;
    using NPoco;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace TestHolidayApi
    {
        class Program
        {
            static void Main(string[] args)
            {
    
    
                //string day = System.DateTime.Today.ToString("yyyyMMdd");
                DateTime dt = new DateTime();
                dt = Convert.ToDateTime("2017-01-02");
    
                //string str  =  GetDayState(day);
                int num = 0;
    
                try
                {
                    while (num < 7200) { 
                        using (IDatabase db = new Database("monitor"))
                        {
    
                            HOLIDAY dy = new HOLIDAY();
                            dy.DAY = dt;
    
                            dy.STATUS = GetDayState(dt.ToString("yyyyMMdd")).ToString();
                            db.Insert<HOLIDAY>("HOLIDAY", "DAY",false, dy);
                            dt = dt.AddDays(1);
    
    
    
                            num++;
                        }
                    }
                }
                catch (Exception ex)
                {
    
                    throw ex;
                }
              
    
                //Console.WriteLine(str);
    
                Console.ReadKey();
           
            }
    
    
            public static string GetDayState(string day)
            {
                string serviceAddress = "http://api.goseek.cn/Tools/holiday?date="+day+"";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
    
                request.Method = "POST";
                request.ContentType = "application/json";
                //string strContent = @"{ ""mmmm"": ""89e"",""nnnnnn"": ""0101943"",""kkkkkkk"": ""e8sodijf9""}";
                //using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
                //{
                //    dataStream.Write(strContent);
                //    dataStream.Close();
                //}
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码  
                }
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                string retString = reader.ReadToEnd();
                //解析josn
                JObject jo = JObject.Parse(retString);
    
                return jo["data"].ToString();
               // Response.Write(jo["message"]["mmmm"].ToString());
            }
        }
    }
    
    
    配置文件部分(App.config)
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="oracle.manageddataaccess.client"
          type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
        </configSections>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
        </startup>
    
      <connectionStrings>
    
        <add  name="monitor" connectionString="Data Source=localhost/ORCL; User Id=monitor_bw; Password=123;" providerName="Oracle.ManagedDataAccess.Client"/>
        
      </connectionStrings>
      
        <system.data>
            <DbProviderFactories>
                <remove invariant="Oracle.ManagedDataAccess.Client"/>
                <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
                    type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
            </DbProviderFactories>
        </system.data>
        <runtime>
            <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                <dependentAssembly>
                    <publisherPolicy apply="no"/>
                    <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/>
                    <bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.122.19.1"/>
                </dependentAssembly>
            </assemblyBinding>
        </runtime>
        <oracle.manageddataaccess.client>
            <version number="*">
                <dataSources>
                    <dataSource alias="SampleDataSource"
                        descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) "/>
                </dataSources>
            </version>
        </oracle.manageddataaccess.client>
    </configuration>
    

    相关文章

      网友评论

          本文标题:C# 调用网页接口

          本文链接:https://www.haomeiwen.com/subject/igbpwctx.html