lu
4 天以前 5a7f68bf4dbbdca1abbb939bd43e1fb695bc7c0b
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
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Cache;
using System.Text;
using System.Threading.Tasks;
 
namespace Gs.WeightIqc.ToolBox
{
    public class UtilityHelper
    {
        private static readonly string WebApiUrl =
          ConfigurationManager.AppSettings["WebApiUrl"];
 
        /// <summary>
        /// http请求
        /// </summary>
        /// <param name="url">api根地址</param>
        /// <param name="meth">方法名称</param>
        /// <param name="param">json参数</param>
        /// <param name="isLoading">是否loading</param>
        /// <returns></returns>
        public static string HttpPost(string url, string meth, string param, bool isLoading = true)
        {
            HttpWebRequest request = null;
            StreamWriter requestStream = null;
            WebResponse response = null;
            string responseStr = null;
            try
            {
                if (string.IsNullOrEmpty(url))
                    url = WebApiUrl;
                url += meth;
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json";
                request.Headers.Add("token", GetBasicAuthTicket());
                request.Accept = "*/*";
                request.Timeout = 150000;
                request.AllowAutoRedirect = false;
                request.ServicePoint.Expect100Continue = false;
                HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
                request.CachePolicy = noCachePolicy;
                requestStream = new StreamWriter(request.GetRequestStream());
                requestStream.Write(param);
                requestStream.Close();
                response = request.GetResponse();
                if (response != null)
                {
                    var reader = new StreamReader(response.GetResponseStream(),
                        Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    //File.WriteAllText(Server.MapPath("~/") + @"\test.txt", responseStr); 
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Debug(url, param + ":" + ex.Message);
                throw ex;
            }
            finally
            {
                request = null;
                requestStream = null;
                response = null;
            }
            return responseStr;
        }
 
 
        /// <summary>
        ///     生成访问服务的token
        /// </summary>
        /// <returns></returns>
        public static string GetBasicAuthTicket()
        {
            var userGuid = "11111111-1111-1111-1111-111111111111";
            var token = userGuid;
            return token;
        }
 
        /// <summary>
        ///     服务返回的json返回ReturnModel,
        /// </summary>
        /// <param name="strReturn"></param>
        /// <returns></returns>
        public static ReturnModel<dynamic> ReturnToDynamic(string strReturn)
        {
            var rto = new ReturnModel<dynamic>();
            var json = JObject.Parse(strReturn);
            rto.rtnCode = int.Parse(json["rtnCode"].ToString());
            rto.rtnMsg = json["rtnMsg"].ToString();
            rto.rtnData = json["rtnData"];
            return rto;
        }
 
 
    }
}