Spotify搜索栏API允许用户搜索艺术家并收到艺术家的前5张专辑



我现在在单击网页上的按钮时会收到错误。

System.Net.WebException
The remote server returned an error: (400) Bad Request.
Description: HTTP 500.Error processing request.
Details: Non-web exception. Exception origin (name of application or object): System.

using System;
    using System.Collections.Specialized;
    using System.Net;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
namespace Task_3
{
    public partial class Default : System.Web.UI.Page
    {
        public void button1Clicked(object sender, EventArgs args)
        {
            button1.Text = "Search";
            string[] hello = {"Hello, World"};
            MainClass.Main(hello);
            MainClass.SetInput(search1.Text);
            TableRow tRow = new TableRow();
            TableRow tRow2 = new TableRow();
            TableCell album1 = new TableCell();
            TableCell artist1 = new TableCell();
            TableCell date1 = new TableCell();
            TableCell tracks1 = new TableCell();
            TableCell popularity1 = new TableCell();
            TableCell id1 = new TableCell();
            TableCell album2 = new TableCell();
            TableCell artist2 = new TableCell();
            TableCell date2 = new TableCell();
            TableCell tracks2 = new TableCell();
            TableCell popularity2 = new TableCell();
            TableCell id2 = new TableCell();
            album1.Text = "Album Name";
            artist1.Text = "Artist Name";
            date1.Text = "Date of Release";
            tracks1.Text = "Number of Tracks";
            popularity1.Text = "Popularity";
            id1.Text = "ID";
            tRow.Cells.Add(album1);
            tRow.Cells.Add(artist1);
            tRow.Cells.Add(date1);
            tRow.Cells.Add(tracks1);
            tRow.Cells.Add(popularity1);
            tRow.Cells.Add(id1);
            Table1.Rows.Add(tRow);
            tRow2.Cells.Add(album2);
            tRow2.Cells.Add(artist2);
            tRow2.Cells.Add(date2);
            tRow2.Cells.Add(tracks2);
            tRow2.Cells.Add(popularity2);
            tRow2.Cells.Add(id2);
            Table1.Rows.Add(tRow2);
            album1.BorderStyle = BorderStyle.Solid;
            artist1.BorderStyle = BorderStyle.Solid;
            date1.BorderStyle = BorderStyle.Solid;
            tracks1.BorderStyle = BorderStyle.Solid;
            popularity1.BorderStyle = BorderStyle.Solid;
            id1.BorderStyle = BorderStyle.Solid;
            Table1.BorderStyle = BorderStyle.Solid;
            Table1.Visible = true;
            id2.Text = MainClass.getData(2);
        }
    }
    class MainClass
    {
        static string input;
        static string [] data = new string[25];
        public static void fillData(){
            for (int j = 0; j < 25; j++)
            {
                data[j] = "" + j.ToString();
            }
        }
        public static string getData(int j){
           // fillData();
            return data[j];
           // return input;
        }
        public static void SetInput(string str){
            input = str;
        }
        public string GetInput(){
            return input;
        }
        public static void Main(string[] args)
        {
            fillData();
            string search = input;//"Muse";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.spotify.com/v1/search?q=" + search + "&type=artist");
            request.Method = "GET";
            request.ContentType = "application/json";
            request.Accept = "application/json";
            request.Headers.Add("Authorization", "Bearer " + "XXXXXXXX");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // causing the error on website
            string myResponse = "";
            using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
            {
                myResponse = sr.ReadToEnd();
                sr.Close();
                response.Close();
            }
            string[] idStrings = new string[50];
            int index1;
            for (int i = 0, startIndex = 0; i < 50; i++)
            {
                idStrings[i] = "";
                index1 = myResponse.IndexOf("id" : "", startIndex);
                if (index1 == -1) break;
                else idStrings[i] = myResponse.Substring(index1 + 7, 22);
                startIndex = index1 + 30;
            }
            string id = "4aawyAB9vmq...";
            HttpWebRequest idRequest = (HttpWebRequest)WebRequest.Create("https://api.spotify.com/v1/albums/" + id);
            idRequest.Method = "GET";
            idRequest.ContentType = "application/json";
            idRequest.Accept = "application/json";
            idRequest.Headers.Add("Authorization", "Bearer " + "XXXXXXXXXXX");
            HttpWebResponse idResponse = (HttpWebResponse)idRequest.GetResponse();
            string myIdResponse = "";
            using (System.IO.StreamReader sr = new System.IO.StreamReader(idResponse.GetResponseStream()))
            {
                myIdResponse = sr.ReadToEnd();
            }
            Console.WriteLine(myResponse.ToString());
            for (int i = 0; i < 50; i++)
            {
                Console.WriteLine(idStrings[i]);
            }

        }

我在这里做错了什么?我评论了http 500.error处理请求的位置,但是我不知道如何修复它。该代码通过终端工作,但不能通过网页进行... ???

我可能会写很多东西,并且可能有一种更简单的方法,但是我以前没有在ASP.NET环境中工作,所以我是新手。

任何帮助将不胜感激!

另外,如果有人可以告诉我如何进行下一步获取输入并从艺术家搜索结果中获取ID,这也会有所帮助!

您的按钮没有绑定到它的单击事件,因此单击它永远不会触发button1Clicked

<asp:Button id="button1" runat="server" Text="Search" OnClick="button1Clicked" />

,此行public static void Main(string[] args)没有参考文献(GetClientCredentialsAuthToken也是如此(,看起来像是winform方法。您确定您不混合WebForms和Winforms代码吗?

相关内容

最新更新