简体   繁体   English

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

[英]The HTTP redirect request failed in UWP C# project

I'm getting this error "The HTTP redirect request failed" when I'm trying to get text (whole string) of this url (for example) : " 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 (I uploaded it to Pastebin) 当我试图获取此URL的文本(整个字符串)时,我收到此错误“HTTP重定向请求失败”(例如):“ 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 “但它在chrome和我工作必须得到json文件https://pastebin.com/GJdLK3BA (我上传到Pastebin)

Platform:UWP x64 System:Windows 10 pro April Update 平台:UWP x64系统: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");

Error Message: The HTTP redirect request failed 错误消息:HTTP重定向请求失败

The Windows.Web.Http.Headers namespace supports creation of HTTP headers and cookies, which are then associated as properties with HttpRequestMessage and HttpResponseMessage objects. Windows.Web.Http.Headers命名空间支持创建HTTP标头和cookie,然后将其作为属性与HttpRequestMessageHttpResponseMessage对象关联。 Please add header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"; 请添加header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"; before send get request like the following. 在发送get请求之前,如下所示。 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);
    }
}

Usage 用法

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

在此输入图像描述 I tried this example and it worked for me. 我试过这个例子,它对我有用。 Using HttpWebRequest and setting a CookieContainer will hopefully solve the problem 使用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)
                {} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM