php - Echo Nest API: convert curl command for checking rate limit to c# -
i'm trying convert curl command c#
curl -i 'http://developer.echonest.com/api/v4/artist/profile?api_key=[api key]&name=weezer'
this returns response header content so:
http/1.1 200 ok content-length: 135 x-ratelimit-limit: 120 x-ratelimit-remaining: 62 x-ratelimit-used: 58
i tried code when run it, gets exception: "http error 405 method not allowed".
string baseurl = "http://developer.echonest.com/api/v4/artist/profile?api_key=[api key]&name=weezer"; httpwebrequest request = (httpwebrequest)webrequest.create(baseurl); request.method = "post"; request.accept = "application/json"; request.useragent = "curl/7.37.0"; request.contenttype = "application/x-www-form-urlencoded"; using (var streamwriter = new streamwriter(request.getrequeststream())) { string data = "browser=win7x64-c1|chrome32|1024x768&url=http://www.google.com"; streamwriter.write(data); } var response = request.getresponse(); string text; using (var sr = new streamreader(response.getresponsestream())) { text = sr.readtoend(); console.writeline(text); }
any appreciated.
as changes daniel suggested, remove useragent. after that, looks pretty c# i'm using successfully. here's shortened version of that:
string baseurl = "http://developer.echonest.com/api/v4/artist/profile?api_key=[api key]&name=weezer"; var req = (httpwebrequest) webrequest.create(baseurl); req.method = webrequestmethods.http.get; req.accept = "application/json"; using (var response = (httpwebresponse) req.getresponse()) { if (response.statuscode == httpstatuscode.ok) { var stream = response.getresponsestream(); if (stream != null) { using (var sr = new streamreader(stream)) { responsestring = sr.readtoend(); } var remaining = getrateinfo(response.headers, "x-ratelimit-remaining"); var used = getrateinfo(response.headers, "x-ratelimit-used"); var limit = getrateinfo(response.headers, "x-ratelimit-limit"); trace.writeline($"excedeed echonest limits: remaining {remaining} - used {used} - limit {limit}"); } } else { // error handling }
// , header parsing code:
private static int getrateinfo(webheadercollection headers, string type) { var s = headers.get(type); if (s == null) return -1; int info; return int.tryparse(s, out info) ? info : -1; }
Comments
Post a Comment