glitch.com项目无法使用C#调用。

huangapple go评论76阅读模式
英文:

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

问题

我写这个问题是关于 glitch.com 项目的上下文。

所以,我在 glitch.com 中创建了一个测试项目(asp.net .net6 webapi),该项目返回您的 IP 地址。网页链接是 https://ror-test.glitch.me/getip。当从浏览器调用时,它运行得非常完美并且返回响应准确。现在,我想从 C# 客户端(准确地说是 Unity3D)访问此项目,但我无法访问它。

我的第一次尝试是使用 httpclient.getstringasync -

代码-

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

输出-

未处理的异常。System.Net.Http.HttpRequestException: 响应状态代码未指示成功:403(禁止)。

在我的第二次尝试中,我使用了 httpclient.getasync -

代码-

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

输出-

响应 - https://jpst.it/348Ik

响应内容 - https://jpst.it/348LS

还要说一句,当我从 Node.js 调用该项目时,我的应用程序运行得非常完美-

代码 -

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();

输出 -

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

在 xx.xx.xxx.xx 的位置,我的真实 IP 地址,我已经通过 https://whatismyipaddress.com/ 进行了交叉检查。所以我确信问题出现在 C# 客户端。

请帮助我,或者是否有其他方法可以从 C# 客户端(精确地说是 Unity3D)调用它。

英文:

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).

答案1

得分: 1

你需要传递“User-Agent”请求头。

HttpClient client = new HttpClient();

// 添加用户代理
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "fake");

var response = await client.GetAsync(url);
英文:

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);

huangapple
  • 本文由 发表于 2023年1月10日 16:58:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75067591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定