duheng
2025-02-11 af0cfb03cda2679b4e89bf42ebea3d06e22ec4c0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using static System.Net.WebRequestMethods;
 
namespace IStation.BLL
{
    public class PredictWater
    {
        //  string apiServiceUrl = "http://47.100.245.85:8188";
 
        /// <summary>
        /// 获取预测水量  (平均数)
        /// </summary>
        /// <param name="StartTime"></param> 开始时间
        /// <param name="EndTime"></param>  结束时间
        /// <param name="days"></param>    取前多少天的数据作为预测数据
        /// <param name="error"></param>
        /// <returns></returns>
        public List<Model.SSAData> GetAnaWaterOneDay(DateTime StartTime, DateTime EndTime, int days, out string error)
        {
            string ApiPredictUrl = System.Configuration.ConfigurationManager.AppSettings["ApiPredictUrl"];
            if (string.IsNullOrEmpty(ApiPredictUrl))
            {
                error = "ApiServiceUrl 为空";
                return null;
            }
            string url = string.Format(@"{0}/Predict/user/TimeBolckWaterAna?starttime={1}&endtime={2}&days={3}", ApiPredictUrl, StartTime, EndTime, days);
            string Accept = "application /json";
            //创建Web访问对象
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            myRequest.Method = "GET";
            //myRequest.Accept = "application/json";
            //myRequest.ContentType = "application/json";  // //Content-Type: application/x-www-form-urlencoded
            myRequest.AutomaticDecompression = DecompressionMethods.GZip;
            myRequest.Accept = Accept;
            //myRequest.ContentType = ContentType;
            myRequest.ContentType = "application/json; charset=UTF-8";
            //myRequest.ContentLength = buf.Length;
            myRequest.MaximumAutomaticRedirections = 1;
            myRequest.AllowAutoRedirect = true;
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
            string returnData = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
            if (string.IsNullOrEmpty(returnData))
            {
                myResponse.Close();
                error = "利用StreamReader就可以从响应内容从头读到尾";
                return null;
            }
            var ret = (new System.Web.Script.Serialization.JavaScriptSerializer()).Deserialize<IStation.Dto.ApiResult<List<Model.SSAData>>>(returnData);
            reader.Close();
            myResponse.Close();
            error = null;
            return ret.Data;
        }
 
        /// <summary>
        /// 获取预测水量  (算法)
        /// </summary>
        /// <param name="StartTime"></param> 开始时间
        /// <param name="EndTime"></param>  结束时间
        /// <param name="days"></param>    取前多少天的数据作为预测数据
        /// <param name="error"></param>
        /// <returns></returns>
        public List<Model.SSAData> GetPredictWaterOneDay(DateTime StartTime, DateTime EndTime, int days, out string error)
        {
            string ApiPredictUrl = System.Configuration.ConfigurationManager.AppSettings["ApiPredictUrl"];
            if (string.IsNullOrEmpty(ApiPredictUrl))
            {
                error = "ApiServiceUrl 为空";
                return null;
            }
            string url = string.Format(@"{0}/Predict/user/TimeBolckWater?starttime={1}&endtime={2}&days={3}", ApiPredictUrl, StartTime, EndTime, days);
            string Accept = "application /json";
            //创建Web访问对象
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            myRequest.Method = "GET";
            //myRequest.Accept = "application/json";
            //myRequest.ContentType = "application/json";  // //Content-Type: application/x-www-form-urlencoded
            myRequest.AutomaticDecompression = DecompressionMethods.GZip;
            myRequest.Accept = Accept;
            //myRequest.ContentType = ContentType;
            myRequest.ContentType = "application/json; charset=UTF-8";
            //myRequest.ContentLength = buf.Length;
            myRequest.MaximumAutomaticRedirections = 1;
            myRequest.AllowAutoRedirect = true;
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
            string returnData = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
            if (string.IsNullOrEmpty(returnData))
            {
                myResponse.Close();
                error = "利用StreamReader就可以从响应内容从头读到尾";
                return null;
            }
            var ret = (new System.Web.Script.Serialization.JavaScriptSerializer()).Deserialize<IStation.Dto.ApiResult<List<Model.SSAData>>>(returnData);
            reader.Close();
            myResponse.Close();
            error = null;
            return ret.Data;
        }
    }
}