我正在开发一个Windows Phone 8应用程序,它从youtube数据api获取视频。我为https://gdata.youtube.com/feeds/api/videos?max-results=10&v=2&alt=jsonc&q=fondoflamenco
创建了一个httpWebRequest,但是我得到了错误远程服务器:找不到。我将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;
}
}
我正在做什么来获取远程服务器错误:未找到
答案 0 :(得分:0)
我刚刚解决了将空凭据添加到https请求的问题,比如
myRequest.Credentials = new NetworkCredential("", "");
这里他解释得更好