【问题标题】:Connecting to .aspx site from c# application从 c# 应用程序连接到 .aspx 站点
【发布时间】:2012-11-20 21:28:00
【问题描述】:

我正在尝试构建 c# 应用程序,当站点中有“更新”时它会通知我。 站点登录表单包含 3 个文本框,它是 login.aspx。

我的问题是,如何将 3 个详细信息“发送”到站点并从我想用 c# 构建的应用程序连接(验证),如果可能的话,我该怎么做? 我查找了任何指南或相关内容,但没有找到。

【问题讨论】:

  • 您需要模拟网络浏览器的行为,例如:身份验证、cookie 和重定向。所以 WebClient 类可以帮助你

标签: c# asp.net login forms-authentication


【解决方案1】:

【讨论】:

  • 谢谢,但是有一个问题。我知道我需要知道页面发送的参数,但是当我尝试使用嗅探器时,我发现内容被加密为很多字母。怎么能这样?
【解决方案2】:

First you need post a form using c#

HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);

            // Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            // Set credentials to use for this request.
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

            Console.WriteLine ("Content length is {0}", response.ContentLength);
            Console.WriteLine ("Content type is {0}", response.ContentType);

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream ();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);

            Console.WriteLine ("Response stream received.");
            Console.WriteLine (readStream.ReadToEnd ());
            response.Close ();
            readStream.Close ();

then try to save cookie, its required to store aspnet_session_id into client for future requests

private class CookieAwareWebClient : WebClient
{
    public CookieAwareWebClient()
        : this(new CookieContainer())
    { }
    public CookieAwareWebClient(CookieContainer c)
    {
        this.CookieContainer = c;
    }
    public CookieContainer CookieContainer { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = this.CookieContainer;
        }
        return request;
    }
}

确保在每个请求上发送并恢复 aspnet_session_id。 还有宾果游戏!

我建议你阅读this

【讨论】:

    猜你喜欢
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2011-04-09
    • 2019-11-07
    • 1970-01-01
    相关资源
    最近更新 更多