gpt4 book ai didi

c# - Pingdom Restful API C#

转载 作者:行者123 更新时间:2023-11-30 12:28:30 26 4
gpt4 key购买 nike

我在使用 Pingdoms API 时遇到问题

我已经为我的应用程序生成了一个 key 并将其添加到我的请求的 header 中。但我不断收到 401 错误代码 :(

NetworkCredential nc = new NetworkCredential("my_email", "my_password", "httpS://api.pingdom.com/2.0/checks");
CredentialCache cc = new CredentialCache();
cc.Add("httpS://api.pingdom.com/2.0/checks", 443, "Basic", nc);

HttpWebRequest request = HttpWebRequest)WebRequest.Create("httpS://api.pingdom.com/2.0/checks");
request.Proxy = new WebProxy("my_proxy",true); //I'm behind a strict firewall...
request.Headers.Add("App-Key", "my_appKey");
request.ContentLength = 0;
request.Credentials = cc;
request.PreAuthenticate = true;
request.Method = "POST";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

我很迷茫:(

无论我做什么,我都会得到:

An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The remote server returned an error: (401) Unauthorized.

工作代码感谢@Guy,我让它工作了!这是我的工作代码,适用于遇到同样问题的任何人

public class PingdomChecks
{
public List<PingdomCheck> Checks { get; set; }
}

public class PingdomCheck
{
public string ID { get; set; }
public int Created { get; set; }
public string Name { get; set; }
public string Hostname { get; set; }
public string Resolution { get; set; }
public int LastErrorTime { get; set; }
public int LastTestTime { get; set; }
public int LastResponseTime { get; set; }
public string Status { get; set; }
public string[] Tags { get; set; }
}

public class Tools
{

public static void SetBasicAuthHeader(WebRequest request, String userName, String userPassword)
{
string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers["Authorization"] = "Basic " + authInfo;
}

public static void Pingdom()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.pingdom.com/api/2.0/checks");
request.Headers.Add("App-Key", "YOUR_APP_KEY");
request.ContentLength = 0;
request.PreAuthenticate = true;
request.Method = "GET";
SetBasicAuthHeader(request, "YOUR_EMAIL", "YOUR_PASSWORD");

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();
var reader = new StreamReader(stream);
var html = reader.ReadToEnd();

PingdomCheck Checks = JsonConvert.DeserializeObject<PingdomCheck>(html);
}

最佳答案

有些服务器希望在不要求的情况下获得身份验证。如果您只使用 request.Credentials = cc; 并且 C# 不会发送它如果您使用 wireshark 并查看它发送到服务器的内容,您会发现缺少 header 。克服这个问题的方法是强制使用身份验证 header 。

//so instead of using:
//request.Credentials = cc;
//request.PreAuthenticate = true;
//you should call:

SetBasicAuthHeader(request, "my_email", "my_password")
}

public void SetBasicAuthHeader(WebRequest request, String userName, String userPassword)
{
string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers["Authorization"] = "Basic " + authInfo;
}

关于c# - Pingdom Restful API C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21482823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com