Spotify搜索栏api,允许用户搜索艺术家并接收艺术家前5张专辑

时间:2018-02-20 17:27:19

标签: c# asp.net webpage spotify

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

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,这也会有所帮助!

1 个答案:

答案 0 :(得分:1)

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

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

这一行public static void Main(string[] args)没有引用(和GetClientCredentialsAuthToken一样),看起来像Winform方法。你确定你没有混合WEBforms和WINforms代码吗?