我正在努力使用WebRequest类来获取下载的句柄URL。除了当我输入https://soundcloud.com/top-bollywood-songs/atif-aslam-mashup-full-song-dj之类的链接(最后一段无扩展)时,它会抛出System.Net.WebException并返回& #34;远程服务器返回错误:(400)错误请求"。
WebResponse response = (WebResponse)request.GetResponse();
建议请!!!
答案 0 :(得分:0)
您可以添加用户代理。以下是一个工作示例:
protected string getHTML(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// ****** Add the this line! ****//
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
string html = readStream.ReadToEnd();
response.Close();
readStream.Close();
return html;
}