ASP.net:获取 HTTPS 数据服务器端?
我之前在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你在哪里创建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.
这是我最终编写的解决问题的代码:
Here's the code that I eventually wrote that solved the problem: