D
D
DancingOnWater2014-07-14 12:00:02
Computer networks
DancingOnWater, 2014-07-14 12:00:02

What could be the reason for getting 401 code after successful authorization?

Good afternoon to all old timers. There is such a task: you need to download certain tle in automatic mode from norad.
Started login, read to dock.
I run the example code (I write on sharps) from here , but it does not work. Writes that it is not authorized.
What happens when I authorize: I write the correct login-password, the answer is empty (which is strange), if I change something, I receive error messages in response (which is expected).
As additional information: I'm sitting behind a proxy that requires authorization. My code:

public class SpaceTrackConnection
    {
        public SpaceTrackConnection()
        {
            client.Encoding = Encoding.UTF8;
        }

        public bool login(string login, string password)
        {
            client.Proxy = WebProxy.GetDefaultProxy();
                client.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
            var data = new NameValueCollection
            {
                { "identity", login },
                { "password", password },
                //{ "query", "class/tle_latest/MEAN_MOTION/13--14/orderby/ORDINAL asc/format/tle/metadata/true"}
            };
            byte[] response;
            try{
                response = client.UploadValues(uriBase + "ajaxauth/login", "POST", data);
            }
            catch{
                return false;
            }
            var responseText = Encoding.UTF8.GetString(response);
                responseText.ToLowerInvariant();
            //response =client.DownloadData(uriBase + "basicspacedata/query/class/tle_latest/MEAN_MOTION/13--14/orderby/ORDINAL asc/format/tle/metadata/true");
            return responseText == "\"\"";
        }

        public void logout()
        {
            var tmp = client.UploadValues(uriBase + "ajaxauth/logout", new NameValueCollection());
        }
        public string sendReqest(string request)
        {
            client.Proxy = WebProxy.GetDefaultProxy();
                client.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
            return Encoding.UTF8.GetString(client.DownloadData(uriBase + request));
        }

        public class WebClientEx : WebClient
        {
            // Create the container to hold all Cookie objects
            private CookieContainer _cookieContainer = new CookieContainer();

            // Override the WebRequest method so we can store the cookie 
            // container as an attribute of the Web Request object
            protected override WebRequest GetWebRequest(Uri address)
            {
                WebRequest request = base.GetWebRequest(address);

                if (request is HttpWebRequest)
                    (request as HttpWebRequest).CookieContainer = _cookieContainer;

                return request;
            }
        }   // END WebClient Class

        WebClientEx client = new WebClientEx();
        string uriBase   = "https://www.space-track.org/";
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DancingOnWater, 2014-07-24
@DancingOnWater

The problem was that I was sending Cookies but not accepting them, the working code is:

public class SpaceTrackConnection
    {
        public SpaceTrackConnection()
        {
            client.Encoding = Encoding.UTF8;
        }

        public bool login(string login, string password)
        {
            try{
                logout();
            }
            catch { }
            
            var data = new NameValueCollection
            {
                { "identity", login },
                { "password", password },
            };
            byte[] response;
            try{
                response = client.UploadValues(uriBase + "auth/login", "POST", data);
            }
            catch{
                return false;
            }
             
            var responseText = Encoding.UTF8.GetString(response);
                responseText.ToLowerInvariant();
            return responseText == "\"\"";
        }

        public void logout()
        {
            var tmp = client.DownloadData(uriBase + "ajaxauth/logout");
        }
        public string sendReqest(string request)
        {
            return Encoding.UTF8.GetString(client.DownloadData(uriBase + request));
        }

        public class WebClientEx : WebClient
        {
            // Create the container to hold all Cookie objects
            private CookieContainer m_cookieContainer = new CookieContainer();

            // Override the WebRequest method so we can store the cookie 
            // container as an attribute of the Web Request object
            protected override WebRequest GetWebRequest(Uri address)
            {
                WebRequest request = base.GetWebRequest(address);

                if (request is HttpWebRequest)
                {
                    var httpRequest = request as HttpWebRequest;
                    ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
                    httpRequest.Proxy = WebProxy.GetDefaultProxy();
                        httpRequest.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
                    httpRequest.CookieContainer = m_cookieContainer;
                }

                return request;
            }

            protected override WebResponse GetWebResponse(WebRequest request)
            {
                var tmp = base.GetWebResponse(request);
                if( tmp is HttpWebResponse)
                {
                    var resopnse = tmp as HttpWebResponse;
                    m_cookieContainer.Add(resopnse.Cookies);
                }
                return tmp;
            }
        }   // END WebClient Class

        WebClientEx client = new WebClientEx();
        string uriBase   = "https://www.space-track.org/";
    }

D
Dmitry Skogorev, 2014-07-23
@EnterSandman

wireshark in the teeth. try first with a browser (or whatever you need), then with your code. look for the difference in the algorithm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question