0

我正在用 asp.net 和 c# 开发一个 web 服务,它显示了poinstreak.com上受密码保护的 XML 文件的内容。我的问题是我似乎无法使身份验证正常工作(我不断获得 401 状态)。关于如何解决这个问题的任何建议?

    protected void dataBind()
    {
        //Variables
        string xmlfile = "http://www.pointstreak.com/xml/GET/schedule_11200.xml";
        string xslfile = "/StyleSheet/XSLT/xslForGETMatches.xsl";

        //Credentials
        NetworkCredential myCred = new NetworkCredential("username", "password");
        CredentialCache myCache = new CredentialCache();
        myCache.Add(new Uri(xmlfile), "Basic", myCred);

        //Set the credentials on the XmlUrlResolver object
        XmlUrlResolver resolver = new XmlUrlResolver();
        resolver.Credentials = myCache;

        //XSL Transformation
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(xslfile, XsltSettings.Default, resolver);
        StringBuilder outfile = new StringBuilder();
        XmlWriterSettings settings = xslt.OutputSettings.Clone(); 
        settings.CheckCharacters = false;
        xslt.Transform(xmlfile, XmlWriter.Create(outfile, settings));
        xmlToHtmlOutput.Text = outfile.ToString(); //An ASP Label
    }
4

1 回答 1

0

您可以使用 HttpWebRequest 获取 xml 的内容并对其进行操作。

WebRequest request = WebRequest.Create(url);
NetworkCredential credential = new NetworkCredential(username, password);

request.Credentials = credential;
request.PreAuthenticate = true;

WebResponse response = request.GetResponse();
Stream Answer = response.GetResponseStream();

StreamReader _Answer = new StreamReader(Answer); 
string content = _Answer.ReadToEnd(); //the string of the whole xml
response.Close();
于 2013-07-22T16:07:16.143 回答