简体   繁体   中英

parsing base64 to server returns null in Json when using JsonConvert.SerializeObject();

So there I was all happy parsing data to my server using FormUrlEncodedContent(); when I decided to add an image converted to string using Base64 and received an error saying that the URL was to long. On research I then changed this to JsonConvert.SerializeObject(); but in my code the Json now return null. Could some one look at my code please and suggest what i should do instead? I did try replacing IEnumerable with the object instead but got the same error The process goes through but all data on the server is null.

the json error is as follows: [0:] Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: M. Path '', line 0, position 0. at Newtonsoft.Json.JsonTextReader.ParseValue () [0x002b3] in :0 at Newtonsoft.Json.JsonTextReader.Read () [0x0004c] in :0 at Newtonsoft.Json.JsonReader.ReadAndMoveToContent () [0x00000] in :0 at Newtonsoft.Json.JsonReader.ReadForType (Newtonsoft.Json.Serialization.JsonContract contract, System.Boolean hasConverter) [0x0004a] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in :0 at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00054] in :0 at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in :0 at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String valu e, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in :0 at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in :0 at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) [0x00000] in :0

 IEnumerable<KeyValuePair<string, string>> queries = new 
List<KeyValuePair<string, string>>()
        {                   
                  new KeyValuePair<string, string>("widget1", Widget1),
                  new KeyValuePair<string, string>("widget2", Widget2),
                  new KeyValuePair<string, string>("widget3", 
Widget3),
                  new KeyValuePair<string, string>("widget4", Widget4),
                  new KeyValuePair<string, string>("widget5", Widget5),
                  new KeyValuePair<string, string>("encoding_string", 
encoding_string)                 
    };


        var jsonString = JsonConvert.SerializeObject(queries);       
        //HttpContent q = new FormUrlEncodedContent(queries);
       HttpContent q = new StringContent(jsonString, Encoding.UTF8, 
"application/json");

        using (HttpClient client = new HttpClient())
        {
            using (HttpResponseMessage response = await client.PostAsync(url, 
q))
            {
                using (HttpContent content = response.Content)
                {

                    string mycontent = await content.ReadAsStringAsync();
                   Console.WriteLine("mycontent1 " + mycontent);



            }

        }
    }
}

Json string as follows i have shortened the image as it was to long for the post

jsonString [{"Key":"widget1","Value":"54"},{"Key":"widget2","Value":"test"},{"Key":"widget3","Value":"test"},{"Key":"widget4","Value":"100"},{"Key":"widget5","Value":"3"},{"Key":"encoding_string","Value":"/9j/4UpuRXhpZgAASUkqAAgAAAANAA8BAgAIAAAAqgAAAAEBBAABAAAAqAcAABIBAwABAAAAAQAAADIBAgAUAAAAsgAAACWIBAABAAAAcgMAABsBBQABAAAAxgAAABoBBQABAAAAzgAAAAABBAABAAAAwA8AABABAgAJAAAA1gAAADEBAgAOAAAA3wAAABMCAwABAAAAAQAAAGmHBAABAAAA7QAAACgBAwABAAAAAgAAAFEEAABzYW1zdW5nADIwMTg6MDc6MTcgMTQ6MTY6NDgASAAAAAEAAABIAAAAAQAAAFNNLUc5NTVGAEc5NTVGWFhVMkNSRjcAIAAAkAcABAAAADAyMjADh6cTKkjEJT22IPaTTeYZZUFrkHKU/wDVQUoUogkd4h3aGZftQXlmtxZCSeuHsV4qc0bgVykrRyJGWjXsjWFaRpgXSR37KzAhmYOfSaG1VrLf/BiNWK0hq9o+9W7zdhUpGAXLeozElOx4PA+I4Vchk7zTRfiRwR471/SSnyVtNULS2RKXEUiK8XWtG5KEkv0CEqfotn8tSrZkTU4ob9d47EFeeb00E9aBRDWdUIkMZjDCb03PZDwhA5POWC5KlGrgUN3/AONzYWDDUO0dkshKESk90JSX7poAUMApDJBrYupdSCcqlRH+OxH81n6kWUvmtrS3a0uXy1wJWjmpQ+oZ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orrKvrsk8rRM/ourSgK9 fujbsyellaio8rKIZYiiklDDKIY+W4JD/5sYgG/wA1Vf0APofN3J/xMTrVuI/g46GSPGxLIHiW4l3KSWLCRAuY7As3MiyuyxPJVkhALxRQP9QOACztmtepYHnY6XItrE5S5QaEkM9u6CC1i5s3MP0O5OS7lqOIs309LP0vzMpTaNmgapHk7j/iMldO0cUMtDGQ2wqkl4r0dhmMrK4Z0GfyGSyGQ+FIacH49GNDXsE+nWViEJatG54bgsXJHBABe/u+w69nzVoWoXWranjimuVa9ucBh0JZ5UKsSqqDynB6jkEcg/fX31xMr5R70EellCLZUtWmUN8srRoV5N+2PIWq6WJ63a1bkjsSxVYopJVrooj7lOEJAABJT9AD2AA+njrWUs5KCWS0sLenLkIVVI+iFYJOkbMFYEv1ZgW59+f1+vr76++mTYnVlV8ExDMSkS0kJSDUOAAW7lHaP/2Q=="}]

因此,一旦确认我的代码是正确的,我就会更深入地研究我的PHP代码,并且我使用的是POST而不是json_decode,而一旦实现了它,一切便开始起作用,感谢您的帮助,以确认没有问题

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