ASP.net:获取 HTTPS 数据服务器端?

发布于 2024-10-22 05:26:30 字数 1426 浏览 1 评论 0原文

我之前在 StackOverflow 上询问过如何解析由我的 ASP.net 应用程序以编程方式下载的 XML。我的意思是用户访问 https://www.example.com/page1.aspx。 page1.aspx 的代码隐藏应该以某种方式下载并解析位于 https://www 的 xml 文件.example.com/foo.xml

我收到了关于如何解析 XML 的很好的答案。然而,我一直不太幸运能够从我的安全 HTTPS 服务器检索 XML。

我正在考虑 https://www.example.com/foo.xml 进行身份验证的情况带有 cookie 的请求。 (第三方系统,不是表单身份验证)。我收到的关于如何下载和解析 XML 的问题的答案建议我使用 System.Net.WebClient 类。我读到必须自定义 WebClient 类才能使用 cookie。因此,我编写了以下代码:

public class WebClientWithCookies : WebClient
{
    private CookieContainer m_container = new CookieContainer();

    public CookieContainer CookieContainer
    {
        get { return m_container; }
        set { m_container = value; }
    }

    public void addCookie(Cookie cookie)
    {
        m_container.Add(cookie);
    }

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

但是,当在 https://www.example.com 收到请求时/foo.xml,请求中没有cookie,所以它不起作用。

我该如何解决这个问题?

I previously asked on StackOverflow how to parse XML downloaded programmatically by my ASP.net application. By this, I mean that the user visits https://www.example.com/page1.aspx. The code-behind for page1.aspx is supposed to somehow download and parse an xml file located at https://www.example.com/foo.xml.

I received good answers about how to parse the XML. However, I've been out of luck with being able to retrieve XML from my secure HTTPS server.

I am looking at a situation where https://www.example.com/foo.xml authenticates requests with a cookie. (third party system, not Forms Authentication). The answer I received to my question about how to download and parse XML suggested that I use the System.Net.WebClient class. I read that the WebClient class must be customized to work with cookies. Therefore, I wrote the following code:

public class WebClientWithCookies : WebClient
{
    private CookieContainer m_container = new CookieContainer();

    public CookieContainer CookieContainer
    {
        get { return m_container; }
        set { m_container = value; }
    }

    public void addCookie(Cookie cookie)
    {
        m_container.Add(cookie);
    }

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

However, when the request is received at https://www.example.com/foo.xml, there are no cookies in the request, and so it doesn't work.

How can I work around this problem?

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

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

发布评论

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

评论(2

魔法少女 2024-10-29 05:26:30

你在哪里创建cookie?这似乎是您所显示的代码中缺少的部分。有一个“HttpCookie”类作为System.Web 名称空间可能有用。

Where are you creating the cookie? That seems to be a missing part from the code you are displaying. There is an "HttpCookie" class as part of the System.Web name space that may be useful.

行雁书 2024-10-29 05:26:30

这是我最终编写的解决问题的代码:

    private XmlDocument getXmlData(string url)
    {
        System.Net.HttpWebRequest rq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
        System.Net.CookieContainer container = new System.Net.CookieContainer();

        for (int i = 0; i < System.Web.HttpContext.Current.Request.Cookies.Count; i++)
        {
            System.Web.HttpCookie httpcookie = System.Web.HttpContext.Current.Request.Cookies[i];
            string name = httpcookie.Name;
            string value = httpcookie.Value;
            string path = httpcookie.Path;
            string domain = "my.domain";
            System.Net.Cookie cookie = new System.Net.Cookie(name, value, path, domain);
            container.Add(cookie);
        }

        rq.CookieContainer = container;
        rq.Timeout = 10000;
        rq.UserAgent = "Asset Tracker Server Side Code";

        System.Net.HttpWebResponse rs = (System.Net.HttpWebResponse)rq.GetResponse();

        System.Text.Encoding enc = System.Text.Encoding.GetEncoding(1252);

        System.IO.StreamReader reader = new System.IO.StreamReader(rs.GetResponseStream());

        System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
        xml.Load(rs.GetResponseStream());
        return xml;
    }

Here's the code that I eventually wrote that solved the problem:

    private XmlDocument getXmlData(string url)
    {
        System.Net.HttpWebRequest rq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
        System.Net.CookieContainer container = new System.Net.CookieContainer();

        for (int i = 0; i < System.Web.HttpContext.Current.Request.Cookies.Count; i++)
        {
            System.Web.HttpCookie httpcookie = System.Web.HttpContext.Current.Request.Cookies[i];
            string name = httpcookie.Name;
            string value = httpcookie.Value;
            string path = httpcookie.Path;
            string domain = "my.domain";
            System.Net.Cookie cookie = new System.Net.Cookie(name, value, path, domain);
            container.Add(cookie);
        }

        rq.CookieContainer = container;
        rq.Timeout = 10000;
        rq.UserAgent = "Asset Tracker Server Side Code";

        System.Net.HttpWebResponse rs = (System.Net.HttpWebResponse)rq.GetResponse();

        System.Text.Encoding enc = System.Text.Encoding.GetEncoding(1252);

        System.IO.StreamReader reader = new System.IO.StreamReader(rs.GetResponseStream());

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