kazelee
4 天以前 2aa6ffae020b4dab66ac30ee4436346aa15ff3cb
util/HttpHelper.cs
@@ -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 = "") {