带有重定向的{C#Json发布请求

时间:2016-07-25 05:50:32

标签: c# json redirect http-post

我正在尝试使用json向此网站发出登录POST请求 Link,并按照重定向。如果登录详细信息错误,我当前的程序工作正常。如果细节错误,我会得到(401)未经授权。'消息,这意味着Post请求是成功的。 但是,我的问题是,如果登录详细信息正确,我会收到错误请求'(400)错误请求'。我不知道为什么会发生这种情况,而我目前仍处于困境。 这是我的代码,我希望有人可以帮助我:

static string url = "https://auth.riotgames.com/authz/auth";
        static string uriString = "";
        static void Main(string[] args)
        {
            var request_check = (HttpWebRequest)HttpWebRequest.Create("https://auth.riotgames.com/authz/auth");
            request_check.Host = "auth.riotgames.com";
            request_check.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0";
            request_check.Accept = "application/json, text/javascript, */*; q=0.01";
            request_check.Headers.Add("Accept-Language", "en-US,en;q=0.5");
            request_check.Headers.Add("Accept-Encoding", "gzip, deflate, br");
            request_check.ContentType = "application/json";
            request_check.Headers.Add("X-Requested-With", "XMLHttpRequest");
            request_check.Referer = "https://auth.riotgames.com/authorize?response_type=code&scope=openid%20email&client_id=merch-store-client&ui_locales=de-DE&login_hint=euw&redirect_uri=https://euw.merch.riotgames.com/de/riot_sso/auth/redirect/";
            var cookieContainer = new CookieContainer();
            request_check.CookieContainer = cookieContainer;
            request_check.Method = "POST";
            request_check.KeepAlive = true;
            request_check.AllowAutoRedirect = false;
            // Account details Senturia:a12365478
            using (var streamWriter = new StreamWriter(request_check.GetRequestStream()))
            {
                string json = "{\"username\":\"Senturia\",\"password\":\"a12365478\",\"remember\":false,\"region\":\"EUW1\",\"language\":\"de_DE\",\"lang\":\"de_DE\"}";
                streamWriter.Write(json);
            }
            try
            {
                // Get the response ...
                using (var webResponse = (HttpWebResponse)request_check.GetResponse())
                {
                    // Now look to see if it's a redirect
                    if ((int)webResponse.StatusCode >= 300 && (int)webResponse.StatusCode <= 399)
                    {
                        uriString = webResponse.Headers["Location"];
                        Console.WriteLine("Redirect to " + uriString ?? "NULL");
                    }
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }



            Console.ReadKey();
        }

1 个答案:

答案 0 :(得分:2)

当HTTP请求失败时,您可以捕获WebException,并从服务器读取响应,因为它可能包含有关请求失败原因的有用信息:

catch (WebException e)
{
    using (var stream = e.Response.GetResponseStream())
    using (var reader = new StreamReader(stream))
    {
        Console.WriteLine(reader.ReadToEnd());
    }
}

在您的情况下,这会打印:

{"error":"invalid_session_id","error_description":"Missing session id."}

所以我猜服务器需要一些session id参数和请求一起发送。请参阅您尝试调用的端点的文档,以获取有关如何执行此操作的更多详细信息。