NodeJS glitch.com 使用c#调用www.example.com项目

wyyhbhjk  于 2023-01-12  发布在  Node.js
关注(0)|答案(1)|浏览(116)

我在www.example.com项目的背景下写这个问题。glitch.com projects.
所以,我在www.example.com做了一个测试项目(asp.net.net6 webapi),它会返回你的ip地址。到网页的链接是https://ror-test.glitch.me/getip。它运行得很好,当从浏览器调用时会准确地返回响应。现在,我想从c#客户端(准确地说是unity3d)访问这个项目,但我无法访问它。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.
我的第一次尝试是使用httpclient.getstringasync-
密码-

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

输出-

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

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

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

输出-
响应-https://jpst.it/348Ik
响应内容-https://jpst.it/348LS
另外,我只想说,我的应用程序工作得非常好,当我从nodejs调用项目时,它工作得非常好-
密码-

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

anauzrmj

anauzrmj1#

您需要传递User-Agent请求标头。

HttpClient client = new HttpClient();

//add user agent
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "fake");

var response = await client.GetAsync(url);

相关问题