Search code examples
c#node.jshttpclientglitch-framework

glitch.com project can't be called with c#


I am writing this question in context of glitch.com projects.

so, i made a test project(asp.net .net6 webapi) in glitch.com which returns your ip address. The link to the webpage is https://ror-test.glitch.me/getip . It runs perfectly fine and returns response accurately when called from a browser. Now, I want to access this project from c# client (to be precise unity3d) however i am unable to access it.

My first try was to use httpclient.getstringasync-

Code-

HttpClient client = new HttpClient();
string response = await client.GetStringAsync(url);
System.Console.WriteLine(response);

Output-

Unhandled exception. System.Net.Http.HttpRequestException: Response status code does not indicate success: 403 (Forbidden).

In my second try i used httpclient.getasync

Code-

HttpClient client = new HttpClient();
var response = await client.GetAsync(url);

Output-

Response - https://jpst.it/348Ik

Response.Content - https://jpst.it/348LS

Also just to say that my app is working perfectly fine when i call the project from nodejs it works perfectly-

Code -

var url = "https://ror-test.glitch.me/getip";

var XMLHttpRequest = require("xhr2");

var xhr = new XMLHttpRequest();
console.log("starting");
xhr.open("GET", url);

xhr.onreadystatechange = function () {
   if (xhr.readyState === 4) {
      console.log(xhr.status);
      console.log(xhr.responseText);
   }};

xhr.send();

Output -

starting
200
your ip: xx.xx.xxx.xx

In place of xx.xx.xxx.xx my real ip which i have cross checked with https://whatismyipaddress.com/ is coming. so i am sure problem is with c# client.

Plz help me or any other way i can call it from c# client(precisely unity3d).


Solution

  • You need to pass User-Agent request header.

    HttpClient client = new HttpClient();
    
    //add user agent
    client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "fake");
    
    var response = await client.GetAsync(url);