【发布时间】:2018-07-31 03:39:56
【问题描述】:
我现在在单击网页上的按钮时收到错误消息。
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,这也会有所帮助!
【问题讨论】:
标签: c# asp.net webpage spotify