简体   繁体   中英

Unable to assign HttpWebResponse.GetResponseStream() to Stream

I am getting jsonstream.Length threw an exception of type System.NotSupportedException error while trying for:

 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);
        }

Also Tried with Webclient but still the same error. Is this because of large response size???

The request URL is: 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();
                    };

Edit: I tried with LINQ :

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

But getting the error ,Could not find an implementation of the query pattern for source type object. Select not found.

My Class Structure is something like this:

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

and

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

Your returned json is not a single object instead it is an array. Since you haven't posted your RootObject and other child classes (I am too lazy to declare them) I will use Json.Net and dynamic keyword

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);
        }
    }

}

EDIT

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
                    });
}

在此输入图像描述

Did you try this?

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

Actually that should be in comment, but I was unable to add one.

Try doing the following by copying it into a memory stream to be able to read the 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);
                }
            }

 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM