代码之家  ›  专栏  ›  技术社区  ›  Rusty Nail

用于远程安全登录的HttpClient

c#
  •  1
  • Rusty Nail  · 技术社区  · 8 年前

    我正在尝试以编程方式登录到远程主机,以便在需要时更新设置。我的登录失败了404,这是我的代码:

    public async void Go(string url)
    {
    
        // Create a Uri:
        Uri uri = new Uri(url);
    
        // Create and configure ClientHandler:
        HttpClientHandler ClientHandler = new HttpClientHandler()
        {
            AllowAutoRedirect = true,
            AutomaticDecompression = DecompressionMethods.GZip,
            ClientCertificateOptions = ClientCertificateOption.Automatic,
            CookieContainer = new CookieContainer(),
            Credentials = null,
            MaxAutomaticRedirections = 7,
            MaxRequestContentBufferSize = 1024 * 2,
            PreAuthenticate = false,
            Proxy = new WebProxy(),
            UseCookies = true,
            UseDefaultCredentials = false,
            UseProxy = false
        };
    
        // Create a new HttpClient and parse in the ClientHandler:
        HttpClient client = new HttpClient(ClientHandler);
    
        // Get the Uri Async, required to get Cookies:
        string html = await client.GetStringAsync(uri);
    
        // Get the Cookies from the Cookie Container:
        CookieCollection collection = ClientHandler.CookieContainer.GetCookies(uri);
    
        // Attempt Login:
        html = await client.GetStringAsync(BuildUri(html)); // 404 occurs here.
        }
    
        private string GetToken(string html)
        {
    
            string look = "ajax_token: '";
            string to = "',";
            int StartIndex = html.IndexOf(look) + look.Length;
    
            html = html.Substring(StartIndex);
            int EndIndex = html.IndexOf(to);
            string token = html.Substring(0, EndIndex);
    
            return token;
        }
    
        private string BuildUri(string html)
        {
    
            string token = GetToken(html);
    
            string Parameters = "/ajax_token=" + token 
                              + "&action=" + "login" 
                              + "&username_login=" + Username 
                              + "&password_login=" + Password;
    
            // Concatinate Uri and Params;
            string URI = "https://www.crazydomains.com.au/ajax/top-shopping-cart" + Parameters;
    
            return (URI);
        }
    

    我登录的Uri是:

    https://www.crazydomains.com.au/
    

    我试图通过代码更新我的dnsa和MX记录,因为我的IP改变了。 使用Fiddler,我为POST构建的Uri似乎是正确的。

    https://www.crazydomains.com.au/ajax/top-shopping-cart/ajax_token=<TOKEN>&action=login&username_login=<USERNAME>&password_login=<PASSWORD>
    

    我不明白为什么我得到一个404,Cookies似乎是正确的和有效的,Uri和凭据似乎是正确的。如果有人能指出我的问题,我做错了什么,我会很高兴的!

    编辑:我也试过:

    // Get the Token:
    string token = GetToken(html);
    
    // Conf Params:
    string Parameters = "ajax_token=" + token
                      + "&action=" + "login"
                      + "&username_login=" + Username
                      + "&password_login=" + Password;
    
    // Post URI:
    string PostURI = "https://www.crazydomains.com.au/ajax/top-shopping-cart/";
    
    // Init Content:
    HttpContent content = new StringContent(Parameters, Encoding.UTF8, "application/json");
    
    // Attempt Login:
    HttpResponseMessage response = await client.PostAsync(PostURI, content);
    

    0 回复  |  直到 8 年前