46,185 questions
HTTPClient - C# --- Fiddler
Markus Freitag
3,791
Reputation points
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
}
Community Center Not monitored
Sign in to answer