Pingdom restful API C# -
i having trouble using pingdoms api
i have generated key application , added header of request. keep getting 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 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 get:
an unhandled exception of type 'system.net.webexception' occurred in system.dll additional information: remote server returned error: (401) unauthorized.
working code @guy got work! heres working code strugling 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); }
}
some servers expect authentication without asking it. , c# not send if use request.credentials = cc; can see missing header if use wireshark , @ being sent server. way overcome force 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; }
Comments
Post a Comment