qinchulong
2025-03-29 039a4a5433e7f80adc88b491b549e5d9486e4f9a
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using WIDESEA_Common.Tools;
using WIDESEA_Core.Utilities;
 
namespace WIDESEA_Core.Utilities
{
    public class HttpHelper
    {
        //private static Logger logger = Logger.getLogger();
        /// <summary>
        /// 带参数的Post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static string PostByPara(string Url, string jsonParas)
        {
            //logger.DebugStart("HttpHelper.PostByPara");
            //logger.DebugStart("Url=" + Url + ",jsonParas=" + jsonParas);
            WriteLog.Info("PostByPara").Write("PostByPara接口调用开始" + Url + jsonParas + DateTime.Now, "PostByPara");
            if (Url != string.Empty && jsonParas != string.Empty)
            {
                try
                {
                    string strURL = Url;
                    HttpWebRequest request;
                    request = (HttpWebRequest)WebRequest.Create(strURL);
                    request.Method = "POST";
                    request.ContentType = "application/json;charset=UTF-8";
                    request.CookieContainer = null;
                    request.Timeout = 30000;
                    string paraUrlCoded = jsonParas;
                    byte[] payload;
                    payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
                    request.ContentLength = payload.Length;
                    Stream writer = request.GetRequestStream();
                    writer.Write(payload, 0, payload.Length);
                    writer.Close();
                    System.Net.HttpWebResponse response;
                    response = (System.Net.HttpWebResponse)request.GetResponse();
                    System.IO.Stream s;
                    s = response.GetResponseStream();
                    string StrDate = "";
                    string strValue = "";
                    StreamReader Reader = new StreamReader(s, Encoding.UTF8);
                    while ((StrDate = Reader.ReadLine()) != null)
                    {
                        strValue += StrDate + "\r\n";
                    }
                    Reader.Close();
                    response.Close();
                    //logger.DebugEnd("result=" + strValue);
                    //logger.DebugEnd("HttpHelper.PostByPara");
                    WriteLog.Info("PostByPara").Write("PostByPara接口调用结束" + Url + strValue + DateTime.Now, "PostByPara");
                    return strValue;
 
                }
                catch (Exception ex)
                {
                    WriteLog.Info("PostByPara").Write($"PostByPara接口调用失败:{Url},{ex.Message}", "PostByPara");
                    //logger.Error("HttpHelper.PostByPara -> " + ErrorCode.Common.cx100001 + " -> " + ErrorCode.Common.cx100001.GetDescription());
                    //logger.Error("HttpHelper.PostByPara -> " + ex.ToString());
                }
            }
            else
            {
                //logger.Error("HttpHelper.PostByPara -> " + ErrorCode.Common.cx100002+ " -> " + ErrorCode.Common.cx100002.GetDescription());
 
            }
            //logger.DebugEnd("result=null");
            //logger.DebugEnd("HttpHelper.PostByPara");
            return null;
        }
        /// <summary>
        /// 带参数的Get请求
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string GetByPara(string Url, Dictionary<string, string> param)
        {
            //logger.DebugStart("HttpHelper.GetByPara");
            //logger.DebugStart("Url=" + Url+ ",param.length="+ (param==null? "0":param.Count.ToString()));
            if (Url != string.Empty)
            {
                try
                {
                    StringBuilder builder = new StringBuilder();
                    builder.Append(Url);
                    if (param != null && param.Count > 0)
                    {
                        int i = 0;
                        foreach (var item in param)
                        {
                            if (i == 0)
                            {
                                builder.Append("?");
                            }
                            else
                            {
                                builder.Append("&");
                            }
                            builder.AppendFormat("{0}={1}", item.Key, item.Value);
                            i++;
                        }
                    }
                    string strURL = builder.ToString();
                    HttpWebRequest request;
                    request = (HttpWebRequest)WebRequest.Create(strURL);
                    request.Method = "GET";
                    request.Timeout = 30000;
                    System.Net.HttpWebResponse response;
                    response = (System.Net.HttpWebResponse)request.GetResponse();
                    System.IO.Stream s;
                    s = response.GetResponseStream();
                    string StrDate = "";
                    string strValue = "";
                    StreamReader Reader = new StreamReader(s, Encoding.UTF8);
                    while ((StrDate = Reader.ReadLine()) != null)
                    {
                        strValue += StrDate + "\r\n";
                    }
                    Reader.Close();
                    response.Close();
                    //logger.DebugEnd("result=" + strValue);
                    //logger.DebugEnd("HttpHelper.GetByPara");
                    return strValue;
                }
                catch (Exception ex)
                {
                    //logger.Error("HttpHelper.GetByPara -> " + ErrorCode.Common.cx100001 + " -> " + ErrorCode.Common.cx100001.GetDescription());
                    //logger.Error("HttpHelper.GetByPara -> " + ex.ToString());
                }
            }
            else
            {
                //logger.Error("HttpHelper.GetByPara -> " + ErrorCode.Common.cx100002+ " -> " + ErrorCode.Common.cx100002.GetDescription());
            }
            //logger.DebugEnd("result=null");
            //logger.DebugEnd("HttpHelper.GetByPara");
            return null;
        }
    }
}