0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
alex dave
  • 115
  • 1
  • 4
  • 10
  • What does not work what are you seeing? – sommmen Aug 29 '23 at 06:22
  • You might be getting an SSL cert error if you are running this in a local test environment. If you just need to test: https://stackoverflow.com/questions/12553277/allowing-untrusted-ssl-certificates-with-httpclient <- see top answer for how to just suppress the SSL cert error. Don't leave in production code though. – B.O.B. Aug 29 '23 at 06:42
  • 1
    You are setting the request headers *after* you already received the response... – derpirscher Aug 29 '23 at 07:49
  • And I'm pretty sure the authorization header is not the correct place to set a value for the host – derpirscher Aug 29 '23 at 07:53
  • @sommmen Exe file closes after I click the button without writing json data and not showing in the datagrid. – alex dave Aug 29 '23 at 10:41
  • @B.O.B. but this need to work in production. – alex dave Aug 29 '23 at 10:42
  • @derpirscher even that didn't work. – alex dave Aug 29 '23 at 13:23
  • @alexdave The SSL bypass I linked is because your code is calling 'https://localhost', and I assumed is not using a valid certificate - I don't think you can have a valid certificate that matches host name 'localhost'. In production you don't want to use the SSL bypass, you want to point to a real domain/server that is configured with a valid certificate issued by a CA. – B.O.B. Aug 30 '23 at 14:46

0 Answers0