我试图将此curl命令转换为c#
ggplot(mpg, aes(class, hwy))+
geom_boxplot(aes(fill = drv))+
geom_jitter(width = .3, aes(fill = drv), shape = 21, color = "white")
这会返回一个响应头,其内容如下:
curl -i 'http://developer.echonest.com/api/v4/artist/profile?api_key=[API KEY]&name=weezer'
我尝试了这段代码,但是当我运行它时,它会出现异常:" HTTP错误405方法不允许"。
HTTP/1.1 200 OK
Content-Length: 135
X-RateLimit-Limit: 120
X-RateLimit-Remaining: 62
X-RateLimit-Used: 58
任何帮助将不胜感激。
答案 0 :(得分:0)
除了Daniel建议的更改之外,我还会删除UserAgent。之后,它看起来非常像C#I成功使用。这是一个稍微缩短的版本:
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
}
//标题解析代码:
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;
}