| | |
| | | } |
| | | } |
| | | |
| | | 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 = "") { |
| | | Console.WriteLine(url); |
| | | WebRequest request = WebRequest.Create(url); |