Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WebClient/HttpWebRequest-WP7从https检索XML_C#_.net_Xml_Windows Phone 7 - Fatal编程技术网

C# 使用WebClient/HttpWebRequest-WP7从https检索XML

C# 使用WebClient/HttpWebRequest-WP7从https检索XML,c#,.net,xml,windows-phone-7,C#,.net,Xml,Windows Phone 7,我试图从服务器检索XML文档,并将其作为字符串本地存储。在desktop.Net中,我不需要这样做,我只是做了: string xmlFilePath = "https://myip/"; XDocument xDoc = XDocument.Load(xmlFilePath); 但是,在WP7上,此返回: Cannot open 'serveraddress'. The Uri parameter must be a relative path pointin

我试图从服务器检索XML文档,并将其作为字符串本地存储。在desktop.Net中,我不需要这样做,我只是做了:

        string xmlFilePath = "https://myip/";
        XDocument xDoc = XDocument.Load(xmlFilePath);
但是,在WP7上,此返回:

Cannot open 'serveraddress'. The Uri parameter must be a relative path pointing to content inside the Silverlight application's XAP package. If you need to load content from an arbitrary Uri, please see the documentation on Loading XML content using WebClient/HttpWebRequest.
因此,我开始使用中的WebClient/HttpWebRequest示例,但现在它返回:

The remote server returned an error: NotFound.
是因为XML是https路径吗?或者因为我的路径不是以.XML结尾?我怎么知道?谢谢你的帮助

代码如下:

    public partial class MainPage : PhoneApplicationPage
{
    WebClient client = new WebClient();
    string baseUri = "https://myip:myport/service";
    public MainPage()
    {
        InitializeComponent();
        client.DownloadStringCompleted +=
            new DownloadStringCompletedEventHandler(
            client_DownloadStringCompleted);
    }

    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        client.DownloadStringAsync
          (new Uri(baseUri));
    }

    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
            resultBlock.Text = "Using WebClient: " + e.Result;

        else
            resultBlock.Text = e.Error.Message;
    }

    private void Button2_Click(object sender, RoutedEventArgs e)
    {
        HttpWebRequest request =
          (HttpWebRequest)HttpWebRequest.Create(new Uri(baseUri));
        request.BeginGetResponse(new AsyncCallback(ReadCallback),
        request);
    }

    private void ReadCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request =
          (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response =
          (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        using (StreamReader streamReader1 =
          new StreamReader(response.GetResponseStream()))
        {
            string resultString = streamReader1.ReadToEnd();
            resultBlock.Text = "Using HttpWebRequest: " + resultString;
        }
    }
}

我觉得你把事情搞得太复杂了。下面是一个非常简单的示例,它通过HTTPS从URI请求XML文档

它以字符串的形式异步下载XML,然后使用
XDocument.Parse()
加载它

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += HttpsCompleted;
        wc.DownloadStringAsync(new Uri("https://domain/path/file.xml"));
    }

    private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);

            this.textBox1.Text = xdoc.FirstNode.ToString();
        }
    }

如果您只是将URL从代码中剪切并粘贴到浏览器上,您会看到什么?一个由Firefox正确格式化的XML页面。就像这样:这几乎就是我最终得到的结果,但是异常仍然发生。这个代码对你有用吗?如果是这样的话,我很确定这一定是因为我的HTTPS证书不可信。@JoeBeez是的,我不想这么说,但“在我的机器上工作”。这可能是一个确定的问题。示例代码还包括指定端口。如果将https指定为协议和非标准端口,这可能就是问题所在。(我知道HTTPS在其他平台上使用默认端口以外的端口时会出现问题。)是的,当我回到家时,端口将是我第二件要检查的事情,为你的帮助干杯。但问题是,如果我们使用WebClient,它将在UI线程中运行。最好使用HttpWebRequest。@Mahantesh-因为Mango,WebClient会在调用它的线程上返回。它不会将响应强制到UI线程,除非它从那里开始。以上是一个最简单形式的例子,而不是最佳实践。