gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
i have written httpClient.Send(uri) how to get string resonse (which server sending back…
- User
i have written httpClient.Send(uri) how to get string resonse (which server sending back as response)?
- Assistant
To get the string response from the server after sending a request using the
httpClient.Send()method in C#, you can use theReadAsStringAsync()method available in theHttpContentclass. Here's an example:using System; using System.Net.Http; using System.Threading.Tasks; namespace Example { class Program { public static async Task Main(string[] args) { using (var httpClient = new HttpClient()) { // Replace with your desired URL var uri = new Uri("https://example.com/api/something"); // Send HTTP request and get HTTP response using (var httpResponse = await httpClient.GetAsync(uri)) { // Ensure the request was successful httpResponse.EnsureSuccessStatusCode(); // Read the response content as a string var responseContent = await httpResponse.Content.ReadAsStringAsync(); Console.WriteLine("Response:"); Console.WriteLine(responseContent); } } } } }