使用基于表单的身份验证从网站读取 XML 文件

发布于 2024-10-20 14:06:37 字数 74 浏览 0 评论 0原文

是否有一种标准方法可以从使用基于表单的身份验证的网站读取 XML 文件?我们想要将该文件读入桌面应用程序。

谢谢-戴夫

Is there a standard way to read an XML file from a website that uses forms based authentication? We want to read the file into a desktop app.

thanks - dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只涨不跌 2024-10-27 14:06:37

如果您正在谈论 ASP.NET 表单身份验证,这将是两个步骤过程:

  1. 向登录页面发送 HTTP 请求,发送用户名和密码并捕获服务器发送的身份验证 Cookie
  2. 向返回 XML 文件的脚本发送 HTTP 请求,随请求一起发送身份验证 Cookie

以下是使用自定义的示例WebClient

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

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address) as HttpWebRequest;
        request.CookieContainer = Cookies;
        return request;
    }
}

class Program
{
    static void Main()
    {
        using (var client = new CookieAwareWebClient())
        {
            client.UploadValues("http://www.foo.com/login.aspx", new NameValueCollection
            {
                { "username", "foo" },
                { "password", "secret" },
            });
            client.DownloadFile("http://www.foo.com/foo.xml", "foo.xml");
        }
    }
}

当然,在现实生活中情况可能是这样更复杂,因为根据站点的不同,您可能需要随请求一起发送 ViewState 和其他 ASP.NET 特定的废话。

If you are talking about ASP.NET Forms Authentication this will be a two step process:

  1. Send an HTTP request to the login page sending the username and password and capturing the authentication cookie sent by the server
  2. Send an HTTP request to the script returning the XML file sending the authentication cookie along with the request

Here's an example using a custom WebClient:

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

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address) as HttpWebRequest;
        request.CookieContainer = Cookies;
        return request;
    }
}

class Program
{
    static void Main()
    {
        using (var client = new CookieAwareWebClient())
        {
            client.UploadValues("http://www.foo.com/login.aspx", new NameValueCollection
            {
                { "username", "foo" },
                { "password", "secret" },
            });
            client.DownloadFile("http://www.foo.com/foo.xml", "foo.xml");
        }
    }
}

Of course in the real life things might be more complex because depending on the site you might need to send along ViewState and other ASP.NET specific crap along with the request.

拥抱我好吗 2024-10-27 14:06:37

在 http 请求中发布凭据,响应中将有一个身份验证 cookie,您必须在下一个请求中重用该身份验证 cookie

post the credentials in an http request, on the response there will be an authentication cookie that you have to reuse for your next requests

甜心小果奶 2024-10-27 14:06:37

您将必须大致按照以下步骤使用 HTTPWebRequest/HTTPWebResponse:

1:使用请求向网站提交用户名和密码
2:保存cookies(我假设cookies将包含登录成功的信息)
3:使用另一个请求(包括这些 cookie)来获取 XML。

要查找初始请求的代码,您需要查看登录页面的源代码以查看提交操作,然后通过您的请求重现该代码。您也许可以使用 fiddler 或 firebug 等来帮助解决这个问题。

You're going to have to use HTTPWebRequest/HTTPWebResponse in roughly the following steps:

1: Use a request to submit the username and password to the website
2: Save the Cookies (I assume the cookies will contain the ok that the login worked)
3: Use another request, including these cookies, to get the XML.

To find the code that for the initial request you will need to look at the source code of the login page to see the submission action, and then reproduce this through your request. You may be able to use fiddler or firebug etc to help working this out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文