Question

I am having an issue deserializing a JSON string from an HTTPS web response. Whenever I attempt to do so, it returns the following error:

Additional information: Invalid JSON primitive: .

(Including the period)

Now, because I know for a fact the server is sending a valid JSON string (Fiddler says so) I believe that the string may be encrypted through HTTPS and that is messing with my deserialization. How can I fix this error and correctly deserialize the json string:

JSON

I attempted using both the .NET framework and Newtsoft's JSON.NET as seen in the code below.

        StringBuilder login = new StringBuilder();
        login.Append("json=%7B%22username%22%3A%22csharpautomaton%22%2C%22password%22%3A%22[redacted password]%22%2C%22remember%22%3A0%7D");
        byte[] logBytes = encoding.GetBytes(login.ToString());

        HttpWebRequest requirejs = (HttpWebRequest)WebRequest.Create(new Uri("https://www.textnow.com/api/sessions"));

        //requirejs.PreAuthenticate = true;
        requirejs.KeepAlive = true;
        requirejs.Method = "POST";
        requirejs.Accept = "Accept: application/json, text/javascript, */*; q=0.01";
        requirejs.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
        requirejs.Headers.Add("Accept-Language", "en-US,en;q=0.8");
        requirejs.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
        requirejs.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";

        Stream sw = requirejs.GetRequestStream();
        sw.Write(logBytes, 0, logBytes.Length);
        sw.Close();
        response = (HttpWebResponse)requirejs.GetResponse();
        Stream stream = response.GetResponseStream();
        StreamReader sr = new StreamReader(stream,true);

        string line = sr.ReadLine();
        //the one I tried second
        TheObject id = JsonConvert.DeserializeObject<TheObject>(line);
        MessageBox.Show(id.ToString());

        //the one I tried first v
        System.Web.Script.Serialization.JavaScriptSerializer deserialize = new System.Web.Script.Serialization.JavaScriptSerializer();
        TheObject jsonObject = deserialize.Deserialize<TheObject>(sr.ReadToEnd());

//the object I used with just .NET
public class TheObject
{
    string id { get; set; } 
    string username { get; set; }
}
//the object I used with JSON.NET
public class TheObject
{
    [JsonProperty("id")]
    public string id { get; set; }

    [JsonProperty("username")]
    public string username { get; set; }
}
Was it helpful?

Solution

As it turns out, the server was sending a compressed response. So, all I needed to do in the end was remove "Accept-Encoding","gzip,deflate,sdch", however I could have manually decompressed it as well, but this was an easier option. @r3mus thanks for all your help, you were wonderful, and frankly, I was amazed at the amount of time you spent helping me, I am very thankful!

OTHER TIPS

Turns out this didn't work either, but was part of the solution:

We determined that TextNow actually requires an escaped JSON string to be passed to the json= key, like so:

json={\"username\":\"csharpautomaton\",\"password\":\"[redacted password]\",\"remember\":0}

More information, though this didn't work:

TextNow is requiring the username parameter and password parameter to be passed as x-www-form-urlencoded content (form value pairs).

You can't pass it the full url-encoded JSON string like that, you need to decode the JSON string into a usable object (you're already doing this with jsonObject) and then pass those parameters.

In the end, you need to have the equivalent of this:

StringBuilder login = new StringBuilder();
login.Append("username=csharpautomation&password=xxxxx");
byte[] logBytes = encoding.GetBytes(login.ToString());

Since you're already implementing the jsonObject in your class(es) (albeit with id/username instead of username/password?) it shouldn't be much of a challenge to rebuild the parameter string once you've decoded the JSON object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top