3
我使用Pingdoms APIPingdom的寧靜API C#
我已經產生爲我的應用密鑰,並將其添加到我的請求的報頭中具有的麻煩。不過,我不斷收到一個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();
Im相當失去:(
無論我做什麼,我得到:
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);
}
}
謝謝@Guy那個訣竅:) – FlyingHippo