2010-08-11 3 views
4

서버에서 XML 문서를 검색하고 로컬로 문자열로 저장하려고합니다. 데스크탑 닷넷에서 내가 필요하지 않았다, 난 그냥했다 그러나WebClient/HttpWebRequest - WP7을 사용하여 https에서 XML 가져 오기

 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. 

그래서 지금 here에서 웹 클라이언트/HttpWebRequest를 예제를 사용하는 방법에 대한 설정하지만, 그것은 다음을 반환합니다 :

The remote server returned an error: NotFound. 

XML이 https 경로이기 때문에 그렇습니까? 또는 내 경로가 .XML로 끝나지 않기 때문에? 어떻게 알 수 있습니까? 어떤 도움을 주셔서 감사합니다. 차라리 당신이 overcomplicated 일을했다고 생각

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; 
     } 
    } 
} 
+0

코드의 URL을 잘라내어 브라우저에 붙여 넣으면 어떻게됩니까? – Lazarus

+0

Firefox에서 올바르게 형식이 지정된 XML 페이지입니다. 이런 식으로 : http://www.w3schools.com/xml/simple.xml – JoeBeez

답변

14

:

여기에 코드입니다. 다음은 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(); 
     } 
    } 
+0

이것은 내가 끝내었던 것입니다. 그러나 예외는 여전히 발생합니다. 이 코드가 효과가 있습니까? 그렇다면 정말 신뢰할 수없는 HTTPS 인증서가 다운되어야합니다. – JoeBeez

+0

@JoeBeez 예, 싫지만 "내 컴퓨터에서 작동합니다". 그것은 인증서 문제 일 수 있습니다. 예제 코드에는 포트 지정도 포함됩니다. 프로토콜로 https를 지정하고 비표준 포트를 지정하면이 문제가 발생할 수 있습니다. (나는 HTTPS가 다른 플랫폼의 기본 포트가 아닌 다른 포트를 사용하는 데 문제가 있다는 것을 알고있다.) –

+0

그렇기 때문에 포트가 내 도움이 될 때까지 집에 갈 때 확인할 두 번째 물건이 될 것입니다. – JoeBeez

관련 문제