我正在开发一个windows phone 8应用程序,从youtube数据api获取视频提要。我做了一个httpWebRequest到https://gdata.youtube.com/feeds/api/videos?max-results=10&v=2&alt=jsonc&q=fondoflamenco
,但我得到错误的远程服务器:未找到。我有windows phone 8设备连接到相同的网络。
这是源代码:
Uri targetUri = new Uri("https://gdata.youtube.com/feeds/api/videos?max-results=10&v=2&alt=jsonc&q=fondoflamenco");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
request.Method = "GET";
request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
ReadWebRequestCallback的代码为
private void ReadWebRequestCallback(IAsyncResult callBackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callBackResult.AsyncState;
try
{
HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callBackResult);
using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
var results = httpwebStreamReader.ReadToEnd();
JObject jsonObject = JObject.Parse(results);
JArray items = JArray.FromObject(jsonObject["items"]);
List<Video> videos = new List<Video>();
foreach (var item in items)
{
Video video = new Video();
video.descripcion = item["description"].ToString();
if (item["player"]["mobile"] != null)
{
video.url = item["player"]["mobile"].ToString();
}
video.imagen = new System.Windows.Media.Imaging.BitmapImage(new Uri(item["thumbnail"]["sqDefault"].ToString()));
videos.Add(video);
}
Dispatcher.BeginInvoke(delegate()
{
MediaList.ItemsSource = videos;
});
}
}
catch (WebException ex)
{
//Dispatcher.BeginInvoke(delegate() { DescriptionBox.Text = ex.Message; });
throw;
}
}
我正在做什么是错误的,以获得远程服务器错误:Not Found
我刚刚解决了它添加空凭证到https请求,像这样
myRequest.Credentials = new NetworkCredential("", "");
这里他解释得更好
http://blog.toetapz.com/2010/11/15/windows-phone-7-and-making-https-rest-api-calls-with-basic-authentication/