@soris1989 כתב בבקשת POST בC# יוצרת שגיאה:
@עידו אתה יכול בקוד להשתמש בסיפריות מוכנות של Logging לתוך קובץ.
הקוד הבא מאפשר את קבלת המחרוזת של ה-curl:
| static async Task UploadTextFileWithPost2() |
| { |
| var url = "https://www.call2all.co.il/ym/api/UploadTextFile"; |
| |
| try |
| { |
| var client = new HttpClient(); |
| |
| var obj = new |
| { |
| token = $"{Username}:{Password}", |
| what = "ivr2:14/text_file.ini", |
| contents = "Some test message" |
| }; |
| |
| var json = JsonConvert.SerializeObject(obj); |
| var content = new StringContent(json, Encoding.UTF8, "application/json"); |
| |
| |
| |
| string curlCommand = GenerateCurlCommand("POST", url, client, content); |
| Console.WriteLine("Generated cURL:"); |
| Console.WriteLine(curlCommand); |
| |
| var responseJ2 = await client.PostAsync(url, content); |
| |
| |
| if (responseJ2.IsSuccessStatusCode) |
| { |
| |
| var responseContent2 = await responseJ2.Content.ReadAsStringAsync(); |
| Console.WriteLine($"Response: {responseContent2}"); |
| } |
| else |
| { |
| Console.WriteLine($"Error: {responseJ2.StatusCode}"); |
| return; |
| } |
| } |
| catch (Exception ex) |
| { |
| Console.WriteLine($"Exception: {ex.Message}"); |
| } |
| |
| await Task.Delay(1000); |
| Console.WriteLine("Async work done!"); |
| } |
| |
| static string GenerateCurlCommand(string method, string url, HttpClient client, HttpContent content) |
| { |
| StringBuilder sb = new StringBuilder(); |
| sb.Append($"curl -X {method} "); |
| |
| |
| foreach (var header in client.DefaultRequestHeaders) |
| { |
| sb.Append($"-H \"{header.Key}: {string.Join("; ", header.Value)}\" "); |
| } |
| |
| |
| if (content != null) |
| { |
| string body = content.ReadAsStringAsync().Result; |
| sb.Append($"-d '{body}' "); |
| } |
| |
| |
| sb.Append(url); |
| |
| return sb.ToString(); |
| } |
| |
המתודה GenerateCurlCommand, פולטת לך string עם ה- curl, את ה-string הזה תדפיס בתוך קובץ log או משהו בסגנון. וזה לגבי בקשות שאתה מריץ מהקוד שלך.
לגבי בדיקת בקשות שנכנסות למערכת שלך, במידה ואתה כותב ב asp.net core, אז תיצור middleware באופן הבא:
| using Microsoft.AspNetCore.Http; |
| using System.Collections.Generic; |
| using System.IO; |
| using System.Threading.Tasks; |
| using Microsoft.Extensions.Logging; |
| |
| public class RequestLoggingMiddleware |
| { |
| private readonly RequestDelegate _next; |
| private readonly ILogger<RequestLoggingMiddleware> _logger; |
| |
| public RequestLoggingMiddleware(RequestDelegate next, ILogger<RequestLoggingMiddleware> logger) |
| { |
| _next = next; |
| _logger = logger; |
| } |
| |
| public async Task InvokeAsync(HttpContext httpContext) |
| { |
| |
| _logger.LogInformation("Request Method: {Method}, Request URL: {Url}", httpContext.Request.Method, httpContext.Request.Path); |
| |
| |
| foreach (var header in httpContext.Request.Headers) |
| { |
| _logger.LogInformation("Header: {Header}: {Value}", header.Key, header.Value); |
| } |
| |
| |
| await _next(httpContext); |
| } |
| } |
| |
בקוד של asp.net (לא CORE), תשים ב-global.asax:
| using System; |
| using System.Web; |
| |
| public class Global : HttpApplication |
| { |
| protected void Application_BeginRequest(object sender, EventArgs e) |
| { |
| HttpContext context = HttpContext.Current; |
| |
| |
| var method = context.Request.HttpMethod; |
| var url = context.Request.Url.ToString(); |
| |
| |
| foreach (var header in context.Request.Headers.AllKeys) |
| { |
| var headerValue = context.Request.Headers[header]; |
| System.Diagnostics.Debug.WriteLine($"Header: {header}: {headerValue}"); |
| } |
| |
| |
| System.Diagnostics.Debug.WriteLine($"Request Method: {method}, Request URL: {url}"); |
| } |
| } |
| |
בשרת שלי אין net (סייבר פאנל מבוסס לינוקס)