从api网站下载.json时出现ResponseStatusLine错误

问题描述 投票:0回答:1

我一直在尝试使用Discogs API制作一个小应用程序供个人使用,以便搜索个人艺术家及其专辑,这些使用非官方的Discogs C#应用程序到目前为止工作。现在的问题是,我可以提取专辑的曲目列表的唯一方法是使用我从每个专辑请求中获得的资源URL(例如,https://api.discogs.com/releases/2890373)。

我尝试从每个URL中提取.json,即使使用适当的标头,我仍然会收到ResponseLine错误。

使用我的使用者密钥和密钥添加了Authorization标头,如下所示:

httpWebRequest.Headers.Add("Authorization", "Discogs key=xxx, secret=yyy");

...添加了一个UserAgent,它仍然无法正常工作。

我在Python中尝试过相同的功能,但它完美无缺,但每次我想要有关相册的信息时,我都不想运行Python应用程序。

private void scrape_button_Click(object sender, EventArgs e) {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.discogs.com/releases/2890373");
            httpWebRequest.Method = WebRequestMethods.Http.Get;
            httpWebRequest.Timeout = 12000;
            httpWebRequest.ContentType = "application/vnd.discogs.v2.html+json";
            httpWebRequest.Headers.Add("UserAgent", "matija_search/0.1");
            string file;
            var response = (HttpWebResponse)httpWebRequest.GetResponse();
            using(var sr = new StreamReader(response.GetResponseStream())) {
                file = sr.ReadToEnd();
            }
        }

这是尝试获取数据的按钮。

import requests
import json
info = requests.get('https://api.discogs.com/releases/2890373')
data = info.json()
with open('data.json', 'w') as f:
    json.dump(data, f)

这是一个python等效实际上有效...

c# json api header httprequest
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.