HTTPClient - C# --- Fiddler

Markus Freitag 3,791 Reputation points
2021-05-13T07:09:14.747+00:00

Hello,
I found a good client. How can I test this under Fiddler? Or another server?

 static void Main(string[] args)  
        {  
            //Task.Run((Func<Task>)( () => Program.PostBasicAsync()));  
            Task.Run((Func<Task>)(() => Program.Test1()));  
  


  public static async Task Test1()  
        {  
            //https://stackoverflow.com/questions/23585919/send-json-via-post-in-c-sharp-and-receive-the-json-returned  
            var payload = new Credentials  
            {  
                Agent = new Agent  
                {  
                    Name = "Agent Name",  
                    Version = 1  
                },  
                Username = "Username",  
                Password = "User Password",  
                Token = "xxxxx"  
            };  
  
            // Serialize our concrete class into a JSON String  
            var stringPayload = JsonConvert.SerializeObject(payload);  
  
            // Wrap our JSON inside a StringContent which then can be used by the HttpClient class  
            var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");  
  
            var httpClient = new HttpClient();  
  
// Do the actual request and await the response  
            var httpResponse = await httpClient.PostAsync("http://localhost/api/path", httpContent);  
  
            // If the response contains content we want to read it!  
            if (httpResponse.Content != null)  
            {  
                var responseContent = await httpResponse.Content.ReadAsStringAsync();  
  
                // From here on you could deserialize the ResponseContent back again to a concrete C# type using Json.Net  
            }  

96227-fiddler1.jpg

96263-fiddler2.jpg

Community Center Not monitored
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.