Windows Phone 7 アプリケーションからこの URL https://valueboxtest.lb.dk/mobile/categoriesから通常の JSON 文字列をダウンロードしようとしています。
WebClient と HttpWebRequest の両方を使用しようとしました。どちらも例外をスローします
“The remote server returned an error: NotFound”
これは WebClient を使用するためのコードです
var webClient = new WebClient();
webClient.DownloadStringCompleted += (client_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri("https://valueboxtest.lb.dk/mobile/categories"));
イベントハンドラはコンテンツを表示するだけですが、e.Result は上記の例外をスローします。
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled) MessageBox.Show(e.Result);
}
HttpWebRequest の場合、私のコードは次のようになります。
var httpReq = (HttpWebRequest)WebRequest.Create(new Uri("https://valueboxtest.lb.dk/mobile/categories"));
httpReq.BeginGetResponse(HTTPWebRequestCallBack, httpReq);
次のコールバックを使用します。
private void HTTPWebRequestCallBack(IAsyncResult result)
{
var httpRequest = (HttpWebRequest)result.AsyncState;
var response = httpRequest.EndGetResponse(result);
var stream = response.GetResponseStream();
var reader = new StreamReader(stream);
this.Dispatcher.BeginInvoke(
new delegateUpdate(update),
new Object[] { reader.ReadToEnd() }
);
}
そしてデリゲートメソッドで
delegate void delegateUpdate(string content);
private void update(string content)
{
MessageBox.Show(content);
}
コンソール アプリケーションで実行する
すべてが正常に機能し、JSON 文字列が問題なく返され、結果をコンソールに出力できます。
WP7では別の URLが機能します
奇妙なことに、URL http://mobiforge.com/rssfeedは、上記の両方のシナリオで実際に正常に機能します。
この問題は、エミュレータと実際のデバイスの両方で発生します。
何が間違っている可能性がありますか?REST サービスは不正な方法でデータを返していますか? あなたが私を助けてくれることを本当に願っています!
注: 同時に Fiddler2 を実行していません!