Frage

I am having trouble using Pingdoms API

I have generated a key for my application and added it to the header of my request. But i keep getting a 401 errorcode :(

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 pretty lost :(

Whatever i do i get:

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

WORKING CODE Thanks to @Guy I got it to work! heres my working code for anyone strugling with the same problem

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);
    }

}

War es hilfreich?

Lösung

some servers expect to get authentication without asking for it. and C# does not send it if you just use request.Credentials = cc; you can see you are missing the Header if you use wireshark and look at what it being sent to the server. The way to overcome this is to force an authentication 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;
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top