I want to know why I can't get a C#http request.

Asked 2 years ago, Updated 2 years ago, 307 views

Regarding the presentation code, I would like to obtain the return value by referring to the reference site A, but I can't get .json well, why is this?The client_name parameter is the same as the one you created on the web.

JsonReaderException: Unexpected character encountered while parsing value: <.Path', line 0, position 0.

//Console.WriteLine(res); displays html successfully, but
An exception occurs when trying to display json.

A: https://docs.joinmastodon.org/methods/apps/
B: https://qiita.com/rawr/items/f78a3830d894042f891b

using Newtonsoft.Json;
usingNewtonsoft.Json.Linq;
using System;
using System.Collections.General;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace test4
{
    internal class program
    {
        static Dictionary <string, string > parameters = new Dictionary <string, string > ( )
        {
            { "client_name", "test"},
            { "redirect_uris", "urn:ietf:wg:oauth:2.0:oob"}
        };

        private static async Taskf()
        {
            System.Net.Http.HttpClient=new System.Net.Http.HttpClient();
            client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0)like Gecko");

            //vart=client.GetAsync($"https://mstdn.jp/api/v1/apps?{wait newFormUrlEncodedContent(parameters).ReadAsStringAsync()}").Result;
            start=client.GetAsync($"https://mstdn.jp/api/v1/apps {wait newFormUrlEncodedContent(parameters).ReadAsStringAsync()}").Result;

            varres=t.Content.ReadAsStringAsync().Result;
            dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(res);


            // Console.WriteLine(res);
            Console.WriteLine(json);



        }

        private static async voidt()
        {
            Task.WaitAll(f());
        }


        static void Main (string[]args)
        {
            t();
            

            Console.ReadKey();
        }
    }
}

c#

2022-11-12 01:10

1 Answers

Because html is html and not json, passing html to JsonConvert.DeserializeObject() will almost cause an exception.There may be no exceptions, but you should still not get the result you expect.

You can see it by looking at https://json.org/, but JSON is

  • { (indicating object start)
  • [ (representing the beginning of the array)
  • " (indicating the start of a string)
  • 0 through 9 (first digit)
  • - (negative sign)
  • t (first letter of true)
  • f (first letter of false)
  • n (first letter of null)

Begins with one of the .In response to the exception message

JsonReaderException: Unexpected character encountered while parsing value: <.Path', line 0, position 0.

As mentioned above, JSON cannot parse it because the first character was <.

Needless to say, JsonConvert.DeserializeObject() expects the JSON string to be entered, so try to pass the JSON string.


2022-11-12 03:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.