【问题标题】:C# API POST similar to CURLC# API POST 类似于 CURL
【发布时间】:2017-07-18 21:53:30
【问题描述】:

我正在尝试使用利用 CURL 的 vultr.com API。我已经通过几个不同的调用成功地完成了对其 API 的 POST 调用,但其中一个特别不起作用。这是他们关于 API https://www.vultr.com/api/ 的文档。

我能够让他们的 server/create 和 firewall/rule_create 使用几乎完全相同的代码正常工作,但它不适用于他们的 server/reboot 命令。现在这里是我已经成功开始工作的他们 API 的帖子的代码。

public static void AddIpToFirewall(string ip,string fireWallGroupId)
{
    string Data = "FIREWALLGROUPID=" + fireWallGroupId + "&direction=in&ip_type=v4&protocol=tcp&subnet=" + ip + "&subnet_size=32&port=80";
    string Reponse = String.Empty;
    StreamWriter Sw = null;
    StreamReader Sr = null;
    try
    {
        HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("https://api.vultr.com/v1/firewall/rule_create");
        Req.Method = "POST";
        Req.ContentType = "application/x-www-form-urlencoded";
        Req.ContentLength = Data.Length;
        Req.Headers.Add(ApiKey);
        using (var sw = new StreamWriter(Req.GetRequestStream()))
        {
            sw.Write(Data);
        }
        Sr = new
        StreamReader(((HttpWebResponse)Req.GetResponse()).GetResponseStream());
        Reponse = Sr.ReadToEnd();
        Sr.Close();
    }
    catch (Exception ex)
    {
        if (Sw != null)
            Sw.Close();
        if (Sr != null)
            Sr.Close();
        Console.WriteLine(ex.Message + "\r\n\r\n'error with vultr...");
    }
}

现在这里是不起作用的代码。它与防火墙 POST 命令几乎相同。

public static void RebootCommand(string subId)
{
    string Data = "SUBID=" + subId;
    string Reponse = String.Empty;
    StreamWriter Sw = null;
    StreamReader Sr = null;
    try
    {
        HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("https://api.vultr.com/v1/server/reboot");
        Req.Method = "POST";
        Req.ContentType = "application/x-www-form-urlencoded";
        Req.ContentLength = Data.Length;
        Req.Headers.Add(ApiKey);
        using (var sw = new StreamWriter(Req.GetRequestStream()))
        {
            sw.Write(Data);
        }
        Sr = new
        StreamReader(((HttpWebResponse)Req.GetResponse()).GetResponseStream());
        Reponse = Sr.ReadToEnd();
        Sr.Close();
    }
    catch (Exception ex)
    {
        if (Sw != null)
            Sw.Close();
        if (Sr != null)
            Sr.Close();
        Console.WriteLine(ex.Message + " error with vultr...");
    }
}

它没有收到错误,但无法重新启动 VM。我已经联系了他们的支持人员,他们说他们的重启命令运行正常,没有任何问题。有没有人以前有过这个问题?谢谢。

【问题讨论】:

  • 执行时是否有任何内容写入控制台?
  • subId确切值是多少?此外,文档声称 API Key 是必需的,但您的代码似乎没有设置它。 vultr.com/api
  • 如何确认 VM 没有重新启动?你检查过虚拟机的状态吗?重新启动可能需要一些时间,它不会在几分之一秒内完成。
  • 我能够使用 get 获得 subId 的确切值,而不会出现任何问题。在调试期间,我暂停并确认它是正确的。 API 密钥由一个常量变量传递。这也是正确的,因为它可以与其他调用完美配合。我确认虚拟机没有通过他们的仪表板重新启动,也没有通过直接控制台连接来重新启动。它永远不会重新启动。

标签: c# api curl post httpwebresponse


【解决方案1】:

我想通了。对于我从他们的 API 完成的所有其他 POST 请求,需要 Req.ContentLength = Data.Length 行。但是无论出于何种原因,必须删除该行才能使重新启动命令起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2015-09-19
    • 2017-08-25
    • 1970-01-01
    相关资源
    最近更新 更多