2014-01-31 65 views
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); 
    } 

}

回答

1

某些服務器希望在不詢問它的情況下獲得身份驗證。而C#不會發送它,如果你只是使用request.Credentials = cc; 如果您使用wireshark並查看它發送到服務器的內容,您可以看到您缺少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; 
    } 
+0

謝謝@Guy那個訣竅:) – FlyingHippo