Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用C获取用户代理头以访问GitHub API#_C#_Github Api - Fatal编程技术网

C# 如何使用C获取用户代理头以访问GitHub API#

C# 如何使用C获取用户代理头以访问GitHub API#,c#,github-api,C#,Github Api,我正在尝试制作一个小应用程序,以将GitHub API的结果获取到文本文件。首先,我尝试将数据获取到控制台。我尝试了许多方法,也引用了许多文档,但我找不到解决此问题的方法 行政法规禁止的请求。请确保您的请求具有用户代理标头()。检查其他可能的原因 这是我使用的示例代码。有人能帮我解决有关用户代理标题的问题吗 using System; using System.Collections.Generic; using System.IO; using System.Linq; using Syst

我正在尝试制作一个小应用程序,以将GitHub API的结果获取到文本文件。首先,我尝试将数据获取到控制台。我尝试了许多方法,也引用了许多文档,但我找不到解决此问题的方法

行政法规禁止的请求。请确保您的请求具有用户代理标头()。检查其他可能的原因

这是我使用的示例代码。有人能帮我解决有关
用户代理标题的问题吗

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            GetGameData().Wait();     
        }
        public static async Task<string> GetGameData()
        {

            var url = "https://api.github.com/users/user?client_id=8763c42f48201b31115f&client_secret=4708b9aea8e35878b9748a016198b81de24352a4";
            using (HttpClient client = new HttpClient())
            {
                    client.BaseAddress = new Uri(url);
                    Console.WriteLine(client.BaseAddress);
                    HttpResponseMessage response = await client.GetAsync(url);
                    string strResult = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(strResult);
                    return strResult;
            } 
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Net.Http;
使用系统文本;
使用System.Threading.Tasks;
名称空间控制台EApp2
{
班级计划
{
静态void Main(字符串[]参数)
{
GetGameData().Wait();
}
公共静态异步任务GetGameData()
{
变量url=”https://api.github.com/users/user?client_id=8763c42f48201b31115f&client_secret=4708b9aea8e35878b9748a016198b81de24352a4";
使用(HttpClient=new HttpClient())
{
client.BaseAddress=新Uri(url);
Console.WriteLine(客户端基本地址);
HttpResponseMessage response=wait client.GetAsync(url);
string strResult=wait response.Content.ReadAsStringAsync();
控制台写入线(strResult);
返回strResult;
} 
}
}
}

对于使用HttpClient,Jimi的评论对我很有用:


client.DefaultRequestHeaders.Add(“用户代理”,@“Mozilla/5.0(Windows NT 10;Win64;x64;rv:60.0)Gecko/20100101 Firefox/60.0”)


此问题的示例使用HttpClient,但如果您想使用WebClient,请参阅此问题:

client.DefaultRequestHeaders.Add(“用户代理”@“Mozilla/5.0(Windows NT 10;Win64;x64;rv:60.0)Gecko/20100101 Firefox/60.0”)我将此添加到代码中,但仍然得到相同的错误尝试添加:
ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12
作为
GetGameData()
方法的第一行。我再次发现了相同的错误。在当前呈现的代码状态下-如果这是您的实际代码-这可能是您可以尝试的全部。您需要添加一个
HttpClientHandler
和其他缺少的部分。而且,这并不是传递身份验证令牌的真正方式。搜索有关如何在
OAuth2
场景中使用HttpClient类进行令牌身份验证的示例。你会发现很多。