UWP C# 项目中的 HTTP 重定向请求失败

The HTTP redirect request failed in UWP C# project

当我尝试获取此 url(例如)的文本(整个字符串)时出现此错误 "The HTTP redirect request failed":“https://apic-desktop.musixmatch.com/ws/1.1/macro.subtitles.get?format=json&q_track=Mi%20Mi%20Mi%20%28Radio%20Edit%29&q_artist=SEREBRO&user_language=en&subtitle_format=mxm&app_id=web-desktop-app-v1.0&usertoken=SECRET TOKEN" but it's worked in chrome and I must get that json file https://pastebin.com/GJdLK3BA(我上传了它到 Pastebin)

平台:UWP x64 System:Windows 10 专业版 4 月更新

public string URLToString(string url)
{
    HttpClient client = new HttpClient();
    HttpResponseMessage response = client.GetAsync(url).Result;
    string result = null;
    if (response.IsSuccessStatusCode)
    {
       result = client.GetStringAsync(url).Result;
    }
    else
    {
       result = null;
    }
    return result;
 }

LyURL = "https://apic-desktop.musixmatch.com/ws/1.1/matcher.lyrics.get?format=json&q_track=" + Uri.EscapeUriString(Title.Text) + "&q_artist=" + Uri.EscapeUriString(Arti.Text) + "&user_language=en&subtitle_format=mxm&app_id=web-desktop-app- v1.0&usertoken=*SECRET TOKEN*";

JObject jObject = JObject.Parse(URLToString(LyURL));

String Lyra = (string)jObject.SelectToken("message.body.lyrics.lyrics_body");

错误消息:HTTP 重定向请求失败

Windows.Web.Http.Headers namespace supports creation of HTTP headers and cookies, which are then associated as properties with HttpRequestMessage and HttpResponseMessage objects. Please add header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"; before send get request like the following. For more detail please refer this document.

public async void SendGetMethod(string url, Action<HttpResponseMessage> resopnse)
{
    HttpClient client = new HttpClient();
    var headers = client.DefaultRequestHeaders;
    string header = "";
    if (!headers.UserAgent.TryParseAdd(header))
    {
        // throw new Exception("Invalid header value: " + header);
    }
    header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
    if (!headers.UserAgent.TryParseAdd(header))
    {
        throw new Exception("Invalid header value: " + header);
    }
    HttpResponseMessage httpResponse = new HttpResponseMessage();
    Uri requestUri = new Uri(url);
    try
    {
        httpResponse = await client.GetAsync(requestUri);
        httpResponse.EnsureSuccessStatusCode();
        resopnse(httpResponse);
    }
    catch (Exception ex)
    {
        Debug.Write("Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message);
    }
}

用法

SendGetMethod("url",(res)=>{
                 var result = res
            });

我试过这个例子,它对我有用。使用 HttpWebRequest 并设置 CookieContainer 有望解决问题

string url = @"https://apic-desktop.musixmatch.com/ws/1.1/macro.subtitles.get?format=json&q_track=Mi%20Mi%20Mi%20%28Radio%20Edit%29&q_artist=SEREBRO&user_language=en&subtitle_format=mxm&app_id=web-desktop-app-v1.0&usertoken=SECRET TOKEN";

string res;

    HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);
                try
                {
                    webReq.CookieContainer = new CookieContainer();
                    webReq.Method = "GET";
                    using (WebResponse response = webReq.GetResponse())
                    {
                        using (Stream stream = response.GetResponseStream())
                        {
                            StreamReader reader = new StreamReader(stream);
                            res = reader.ReadToEnd();
                          }
                    }
                }
                catch (Exception ex)
                {}