无法将HttpWebResponse.GetResponseStream()分配给Stream

时间:2012-08-28 09:59:17

标签: c# .net json stream webclient

我正在 jsonstream.Length抛出System.NotSupportedException类型的异常 尝试时出错:

 HttpWebRequest jsonHTTPRequest = (HttpWebRequest)WebRequest.Create(jsonRequestURL);
        jsonHTTPRequest.ContentType = "application/json; charset=utf-8";

        HttpWebResponse jsonHTTPResponse = (HttpWebResponse)jsonHTTPRequest.GetResponse();
        RootObject vikiRootObject = default(RootObject);

        using (Stream jsonstream = jsonHTTPResponse.GetResponseStream())
        {
            //encoding encode = system.text.encoding.getencoding("utf-8");
            DataContractJsonSerializer serializer =
                                     new DataContractJsonSerializer(typeof(RootObject));
            vikiRootObject = (RootObject)serializer.ReadObject(jsonstream);
        }

还尝试使用Webclient,但仍然出现同样的错误。 这是因为响应量大吗???

请求网址为: http://www.viki.com/api/v2/channels.json

WebClient webClient = new WebClient();
                RootObject vikiChannelData = default(RootObject);
                webClient.OpenReadAsync(new Uri(jsonRequestURL), UriKind.RelativeOrAbsolute);

                webClient.OpenReadCompleted += (obj, Args) =>
                    {
                        //DataContractJsonSerializer vikiChannelerialized = new DataContractJsonSerializer(typeof(RootObject),null);
                        DataContractJsonSerializer vikiChannelerialized = new DataContractJsonSerializer(typeof(RootObject));
                        vikiChannelData = vikiChannelerialized.ReadObject(Args.Result) as RootObject;
                        Console.WriteLine();
                    };

修改 我试过LINQ:

var RootObjects = from vikiroot in vikiRootObject
                                  select new Thumbnails2
                                  {
                                      thumbnails = vikiRootObject.thumbnails
                                  };
  

但是得到错误,无法找到源类型对象的查询模式的实现。选择未找到。

我的班级结构是这样的:

  public class RootObject
    {
        public int id { get; set; }
        public string title { get; set; }
        public string description { get; set; }
        public string uri { get; set; }
        public List<Episode> episodes { get; set; }
        public Thumbnails2 thumbnails { get; set; }
        public string timestamp { get; set; }
        public List<object> genres { get; set; }
        public string origin_code { get; set; }
    }

public class Thumbnails2
    {
        public string c_220_160 { get; set; }
        public string c_102_102 { get; set; }
        public string c_180_130 { get; set; }
        public string c_110_80 { get; set; }
        public string xl { get; set; }
        public string large { get; set; }
        public string medium { get; set; }
        public string small { get; set; }
        public string c_320_300 { get; set; }
        public string c_640_600 { get; set; }
        public string c_95_70 { get; set; }
        public string c_190_140 { get; set; }
        public string c_280_200 { get; set; }
        public string c_560_400 { get; set; }
    }

3 个答案:

答案 0 :(得分:2)

您返回的json不是单个对象,而是一个数组。由于您尚未发布RootObject和其他子类(我太懒了,不能声明它们),我将使用Json.Netdynamic关键字

using (var wc = new WebClient())
{
    var json = wc.DownloadString("http://www.viki.com/api/v2/channels.json");
    dynamic dynObj = JsonConvert.DeserializeObject(json);

    foreach (var item in dynObj)
    {
        Console.WriteLine(item.title);
        foreach (var episode in item.episodes)
        {
            Console.WriteLine("\t" + episode.title);
        }
    }

}

修改

using (var wc = new WebClient())
{
    var json = wc.DownloadString("http://www.viki.com/api/v2/channels.json");
    var rootObj = JsonConvert.DeserializeObject<RootObject[]>(json);

    var obj = rootObj.Select(r=>new 
                    {
                        Title = r.title,
                        Thumbnail = r.thumbnails.small
                    });
}

enter image description here

答案 1 :(得分:1)

你试过这个吗?

using (StreamReader reader = new StreamReader(jsonHTTPResponse.GetResponseStream()))
        {
             //your code goes here
        }

实际上应该是评论,但我无法添加。

答案 2 :(得分:-1)

尝试通过将其复制到内存流中来执行以下操作,以便能够读取Stream:

using (Stream jsonstream = jsonHTTPResponse.GetResponseStream()) 
        { 
            DataContractJsonSerializer serializer = 
                                     new DataContractJsonSerializer(typeof(RootObject)); 

             using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonstream)))
            {
                vikiRootObject = (RootObject)serializer.ReadObject(jsonstream); 

                using (MemoryStream ms2 = new MemoryStream())
                {
                    ser.WriteObject(ms2, vikiRootObject);
                    string serializedJson = Encoding.UTF8.GetString(ms2.GetBuffer(), 0, (int)ms2.Length);
                }
            }

 }