I am using Curl script in server and able to get the JSON data from GET link of the API. If I am using the C# code in the Application, it's not working to get the same JSON data. I created my own GET API link in .Net Core and used in my local machine. It works fine, but the same code in server it's not working. Am I doing mistakes anywhere?
curl script:
curl -H "Host: enabler-api.com.baeldung" https://localhost:8080/api/IQueue
using System.IO;
using NewtonSoft.Json;
using System.Net.Http.Json;
private async void btn1_Click(object sender, RoutedEventArgs e) {
HttpClient client = new HttpClient();
string filepath = "C:/Desktop/response1.txt";
var response = await client.GetAsync("https://localhost:8080/api/IQueue");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Host", "enabler-api.com.baeldung");
response.EnsureSuccessStatusCode();
JsonSerializer Serializer = new JsonSerializer();
if (response.IsSuccessStatusCode) {
//parse json, convert json to string
using(StreamWriter sw = new StreamWriter(filepath))
using(JsonWriter writer new JsonTextWriter(sw)) {
var rt = response.Content.ReadFromJsonAsync < IEnumerable < iqueue >> ().Result;
Serializer.Serialize(writer, rt);
MessageBox.Show(rt.ToList().ToString());
usergrid.ItemsSource = rt;
}
} else {
MessageBox.Show("Error Code" + response.StatusCode +: Message - "+ response.ReasonPhrase);
}
}
If I do some code changes I can see that, in text file it is not writing JSON data but it writes as System.Net.Http.StreamContent
. To make this application run in server I need to run batch file. Sometimes in batch command prompt, it shows JSON. In exe file or text file writer it is not showing JSON as output.