kazelee
5 天以前 656f90b301ba307c909de4ee94b88af817adf0d8
util/HttpHelper.cs
@@ -5,7 +5,7 @@
using System.Net;
using System.Text;
namespace HH.WCS.Mobox3.AnGang.config {
namespace HH.WCS.Mobox3.AnGang.util {
    public class HttpHelper {
        public string WebPost(string url, string postData, string cotentType = "application/json")
        {
@@ -101,7 +101,40 @@
                if (stream != null) stream.Close();
                if (rsp != null) rsp.Close();
            }
        }
        public string GetNew(string url, string contentType = null) {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            // 只有指定了contentType时才设置
            if (!string.IsNullOrEmpty(contentType)) {
                request.ContentType = contentType;
            }
            try {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(stream)) {
                    return reader.ReadToEnd();
                }
            }
            catch (WebException ex) when (ex.Response is HttpWebResponse response) {
                // 记录详细的错误信息
                LogHelper.Info($"GET请求失败。状态码: {response.StatusCode}, URL: {url}", "API");
                // 读取错误响应内容
                using (Stream stream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(stream)) {
                    string errorResponse = reader.ReadToEnd();
                    LogHelper.Info($"错误响应内容: {errorResponse}", "API");
                }
                return string.Empty;
            }
            catch (Exception ex) {
                LogHelper.Info($"GET请求失败。URL: {url}, 错误: {ex.Message}", "API");
                return string.Empty;
            }
        }
        public string Post(string url, string postData, string contentType = "application/json", string sessionId = "") {