我试图从服务器检索XML文档并将其作为字符串本地存储。在桌面的.Net我没必要,我只是做:使用WebClient/HttpWebRequest从https检索XML - WP7
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.
所以我开始使用Web客户端/ HttpWebRequest的例子来自here,但现在它返回:
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;
}
}
}
如果您只是将代码中的网址剪切并粘贴到浏览器中,您会看到什么? – Lazarus 2010-08-11 11:50:16
由Firefox正确格式化的XML页面。像这样:http://www.w3schools.com/xml/simple.xml – JoeBeez 2010-08-11 11:54:51