Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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# 如何使用API键调用c中的API_C# - Fatal编程技术网

C# 如何使用API键调用c中的API

C# 如何使用API键调用c中的API,c#,C#,所以我最近第一次尝试使用api,但我真的不知道如何使用这个api:。我已经看过了如何在c中调用api的教程,但我仍然不知道在我的具体情况下我必须做什么。 我试过这个: 这个问题对于那些知道如何处理这样的东西的人来说似乎很愚蠢,但到目前为止,我还没有api方面的经验 我尝试过的确切代码: private const string URL = "https://api.skinbaron.de/"; private static string urlParameters = "?ap

所以我最近第一次尝试使用api,但我真的不知道如何使用这个api:。我已经看过了如何在c中调用api的教程,但我仍然不知道在我的具体情况下我必须做什么。 我试过这个:

这个问题对于那些知道如何处理这样的东西的人来说似乎很愚蠢,但到目前为止,我还没有api方面的经验

我尝试过的确切代码:

    private const string URL = "https://api.skinbaron.de/";
    private static string urlParameters = "?api_key=123"; // I have replaced the "123" with my apikey

    static void Main(string[] args)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(URL);

        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = client.GetAsync(urlParameters).Result;  
        if (response.IsSuccessStatusCode)
        {

            // do something
        }
        else
        {
            Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
        }
        client.Dispose();
    }
你可以用。它真的很容易使用。这是在c中用于管理API的第三方库。不需要为调用API的任何其他细节而烦恼

var client = new RestClient("https://api.skinbaron.de/GetBalance");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Accept", "application/json");
request.AddParameter("apikey", "123");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
//Rest of the stuff

看起来他们希望您发布而不是获取,并在发布的正文中传递一个JSON对象,将apikey作为JSON对象的属性

通常,在C中,您将创建一个模型类,然后为post序列化该类,但是如果您只需要post一个apikey,那么我将只序列化一个字典对象,并将apikey作为集合的唯一成员

例如,我认为这段代码可能会满足您的需求

    private const string URL = "https://api.skinbaron.de/";
    private static string urlParameters = "?api_key=123"; // I have replaced the "123" with my apikey
    private static string apiKey = "";

    static void Main(string[] args)
    {
        using (var webClient = new WebClient()) {
            webClient.Headers.Add(HttpRequestHeader.Accept, "application/json");
            webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");

            var postDictionary = new Dictionary<string, string>() {
                {"apikey", apiKey}
            };

            var responseBody = webClient.UploadString(URL, JsonConvert.SerializeObject(postDictionary));
        }
    }

使用诸如wireshark或fiddler之类的嗅探器捕获http消息以查看格式。Http有一个头和一个正文。参数是标题中的html标记,post是正文中的数据。由于api使用的是Swagger OpenAPI,您可以使用生成c代码。@Crowcoder执行安装程序后,我应该立即启动它吗?因为我不能启动它。对不起,我没有看一眼。我误解了。现在我已经更新了我的应答器返回的错误列表项:检查您的json输入。我已经更新了标题,请查看并尝试一下。我已经在swagger上试用过了,由于apikeyError,它给了我未经授权的列表项,现在应该删除了。它确实删除了那个错误,但现在它显示了错误的真实性标记。文档确实告诉您必须发送特定的头请求,但这似乎不起作用。有什么想法吗?