新框架PC后端代码(祈禧6月初版本)
南骏 池
3 天以前 72449a1b8699b65712e57fba8abce5a8240e9465
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
using System.Net.Cache;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using System.Data.SqlClient;
 
 
namespace Gs.Toolbox;
 
public class InterfaceUtil
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="param">json参数</param>
    /// <param name="edtUserGuid"></param>
    /// <param name="abtGuid"></param>
    /// <param name="hNo"></param>
    /// <param name="urlType">如果为2,则是更新工单状态</param>
    /// <returns>如果成功返回日志guid,否则返回串</returns>
    public static (int, string) HttpPostErp(string param, string edtUserGuid = "", string abtGuid = "", string hNo = "", int urlType = 0)
    {
        System.Text.StringBuilder sbTm = new System.Text.StringBuilder();
        sbTm.Append(DateTime.Now.ToString() + "开始发送");
        string strLogGuid = Guid.NewGuid().ToString();
        string url = AppSettingsHelper.getValueByKey("TestErpUrl");
        if (urlType == 2)
            url = AppSettingsHelper.getValueByKey("TestErpUrl2");
        HttpWebRequest request = null;
        StreamWriter requestStream = null;
        WebResponse response = null;
        string responseStr = "";
        try
        {
            request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";
            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();
                reader.Close();
            }
        }
        catch (Exception ex)
        {
            LogHelper.Debug(url, "HttpPostErp param:" + param + ",ex:" + ex.Message);
            responseStr = ex.Message;
            // throw ex;
        }
        finally
        {
            request = null;
            requestStream = null;
            response = null;
        }
        Result _result = JsonConvert.DeserializeObject<Result>(responseStr);
        int _rtn = 0;
        if ("200".Equals(_result.state))
            _rtn = 1;
        sbTm.Append("," + DateTime.Now.ToString() + "结束发送");
        if (_rtn > 0)
            sbTm.Append(",发送成功");
        else
            sbTm.Append(",发送失败,mes退出操作");
        try
        {
            SqlParameter[] parameters =
    {
            new("@edtUserGuid", edtUserGuid),
            new("@abtGuid", abtGuid),
            new("@abtTable", ""),
            new("@detail", sbTm.ToString()),
            new("@hNo", hNo),
            new("@RtnLogGuid", strLogGuid),
            new("@SendJson", param),
            new("@RtnJson", responseStr),
        };
            DbHelperSQL.RunProcedure("[prc_log_create]", parameters);
        }
        catch (Exception ex)
        {
            LogHelper.Debug(url, "HttpPostErp 写入日志表" + ex.Message);
        }
        return (_rtn, (_rtn > 0 ? strLogGuid : responseStr));
    }
}
 
public class Result
{
    /// <summary>
    /// 200成功,否则失败
    /// </summary>
    public string? state { get; set; }
    public string? msg { get; set; }
 
    public string? status { get; set; }
 
    public string? message { get; set; }
}