RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1348473
Accepted
NastyStuffy
NastyStuffy
Asked:2022-04-11 15:01:32 +0000 UTC2022-04-11 15:01:32 +0000 UTC 2022-04-11 15:01:32 +0000 UTC

这个 PHP Curl 代码在 c# 中会是什么样子?

  • 772
$curl = curl_init("https://api.vimeworld.ru/online");
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  'Access-Token: MY_TOKEN'
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);
curl_close($curl);

print $response;
c#
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    aepot
    2022-04-11T15:26:06Z2022-04-11T15:26:06Z

    我们采用 Visual Studio 2019(或 2022),创建一个 .NET 5(或 6)控制台项目,从服务器响应中获取 json 文本

    {"total":2375,"separated":{"ann":26,"bb":66,"bp":24,"bw":170,"cp":0,"dr":15,"duels":48,"gg":24,"hg":3,"kpvp":21,"lobby":670,"mw":55,"prison":549,"sw":91,"murder":60,"bridge":20,"jumpleague":0,"paintball":0,"turfwars":0,"sheep":0,"tntrun":16,"tnttag":0,"luckywars":279,"zombieclaus":0,"hide":0,"speedbuilders":55,"teamfortress":0,"fallguys":0,"eggwars":183}}
    

    复制,在 studio 菜单中选择Edit - Paste Special - Paste JSON as classes,将完成的数据模型插入​​到代码中,为方便起见重命名其中一个类,我们得到。

    public class OnlineResponse
    {
        public int total { get; set; }
        public Separated separated { get; set; }
    }
    
    public class Separated
    {
        public int ann { get; set; }
        public int bb { get; set; }
        public int bp { get; set; }
        public int bw { get; set; }
        public int cp { get; set; }
        public int dr { get; set; }
        public int duels { get; set; }
        public int gg { get; set; }
        public int hg { get; set; }
        public int kpvp { get; set; }
        public int lobby { get; set; }
        public int mw { get; set; }
        public int prison { get; set; }
        public int sw { get; set; }
        public int murder { get; set; }
        public int bridge { get; set; }
        public int jumpleague { get; set; }
        public int paintball { get; set; }
        public int turfwars { get; set; }
        public int sheep { get; set; }
        public int tntrun { get; set; }
        public int tnttag { get; set; }
        public int luckywars { get; set; }
        public int zombieclaus { get; set; }
        public int hide { get; set; }
        public int speedbuilders { get; set; }
        public int teamfortress { get; set; }
        public int fallguys { get; set; }
        public int eggwars { get; set; }
    }
    

    然后代码将如下所示

    private static readonly HttpClient client = new HttpClient() { DefaultRequestVersion = HttpVersion.Version20 };
    
    static async Task Main(string[] args)
    {
        using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://api.vimeworld.ru/online");
        //сервер отвечает без токена нормально, но если надо добавить, то вот так можно
        //request.Headers.Add("Access-Token", "MY_TOKEN");
        using HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
        OnlineResponse result = await response.Content.ReadFromJsonAsync<OnlineResponse>();
    
        Console.WriteLine(result.total);
        Console.WriteLine(result.separated.lobby);
    
        Console.ReadKey();
    }
    

    控制台输出

    2407
    647
    
    • 2

相关问题

  • 使用嵌套类导出 xml 文件

  • 分层数据模板 [WPF]

  • 如何在 WPF 中为 ListView 手动创建列?

  • 在 2D 空间中,Collider 2D 挂在玩家身上,它对敌人的重量相同,我需要它这样当它们碰撞时,它们不会飞向不同的方向。统一

  • 如何在 c# 中使用 python 神经网络来创建语音合成?

  • 如何知道类中的方法是否属于接口?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5