0

I've searched all the existing question/answers concerning the error in the subject but the behaviour let me thing is not something wrong at the code rather on the machine instead.

I have the local dev machine on Windows 10 in which the deserialization works perfectly. Once I publish on Server 2012 it blows up. I used 3 version of the code to try to force a resolution, I can get different error messages but the result is the same, on production when try to deserialize blows up

I am using .NET framework 4 and C# with NewtonJson to handle JSON calls.

What I am unable to find if someone had odd behaviour on different deploy.

below the snippet:

foreach(var s in ids) {
    i++;
    string _endpoint = sbc_url + s;

    Uri _uri = new Uri(_endpoint);
    WebClient wcClient = new WebClient();

    wcClient.BaseAddress = _endpoint;
    wcClient.Headers.Add("contentType: 'application/json; charset=utf-8'");
    wcClient.Headers.Add("dataType: 'json'");

    var response = wcClient.DownloadString(_endpoint);

    try {
        var jss = new JavaScriptSerializer();
        var dict = jss.Deserialize <Dictionary<string, dynamic>> (response); // BLOWS UP HERE
        ws_ret r = new ws_ret();
        foreach(var tt in dict["result"]) {
            r.result.Add(tt);
        }

        if (r.result != null)
            numeri.result.AddRange(r.result);
    } catch (Exception ex) {

    }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Fabio Guerrazzi
  • 164
  • 1
  • 5
  • 1
    "Does not" means what? Error? Unexpected output? – tadman Dec 07 '17 at 17:13
  • 1) You're not using NewtonJson, you're using [`JavaScriptSerializer`](https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx). 2) Can you [edit] your question to show the JSON that "blows up" as well as the `ToString()` output of the exception including the traceback, message, exception type and inner exception? 3) Possibly the server can't access the `_endpoint` URL for security reasons and you're gettinjg a `WebException`? [Catch it](https://stackoverflow.com/q/5999215/3744182) and see. – dbc Dec 07 '17 at 17:45
  • thanks for reply, I am sorry I was a bit hurry I will extend the question properly. I am not using NewtonJson but I have other version of code using it, As I told the question is about the strange behaviour on different machine, the code is misleading, is useless. The assemblies are the same, the returning json from the external ws is the same, the access is granted (tested manually), I've spent the whole day figuring out why two identical machines one is working and the other does not. btw I'll try to clarify the question ASAP. – Fabio Guerrazzi Dec 07 '17 at 20:02

2 Answers2

0

Setting contentType: "application/json", the request body will treated as JSON content. This is why "Invalid JSON primitive" error occurs, because of URL encoded format is not same as JSON format.

Remove:

wcClient.Headers.Add("contentType: 'application/json; charset=utf-8'");
Salomon Zhang
  • 1,553
  • 3
  • 23
  • 41
0

I apologize but I've solved. The sysadmin didnt inform me that the connection was under firewall and simply the server machine was out of allowed addresses. BTW I am sure this post still make sense because as I was thinking two identical configurations there are no way to have different behaviour and if someone happen the same issue need to insist to investigate everything. As I suspected the error message returned "Invalid JSON primitive" does not have anything to see with the permission denyed. Thanks a lot to everybody

Fabio Guerrazzi
  • 164
  • 1
  • 5