UriFormatException: Invalid URI: Failed to determine URI format, I want to know how to work with

Asked 2 years ago, Updated 2 years ago, 363 views

Regarding the presentation code, I would like to know why the following exceptions occur.I entered the URL from the API site and set the parameters, but why is the exception of invalid?I wrote the source code with reference to the reference site, but I don't know the cause.
I think the redirect URL is probably different, but which one is wrong?

The Console.WriteLine(url); section is printed as follows, so the URL is definitely coming.

https://mstdn.jp/api/v1/apps?client_name=test&redirect_uris=https%3A%2F%2Fmstdn.jp%2Fauth%2Fsign_in

I would like to know how to configure the redirect URL correctly and why the following exceptions occur:

The presentation code registers the client application that can be used to retrieve the OAuth token using the mastodon api of the reference site. code.
The GetLastUrl function is the function that retrieves the redirect URL.

var requ=WebRequest.Create(q);In the code section

UriFormatException—The invalid URI:URI format could not be determined.

API:https://docs.joinmastodon.org/methods/apps/
Source code: https://argius.hatenablog.jp/entry/20131113/1384352397


        public static async Task<string>GetLastUrl (string url)
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0)like Gecko");

            HttpResponseMessage response=wait client.GetAsync(url);
            response.EnsureSuccessStatusCode();

            return response.RequestMessage.RequestUri.ToString();
        }

        private static async Task test_run()
        {
            // Set redirect URL
            // Console.WriteLine("Ah");
            Task<string>task=GetLastUrl("https://mstdn.jp/web/home");
            // Task <string > task = GetLastUrl("https://mstdn.jp/api/v1/apps");
            task.Wait();

            // Set parameters
            Dictionary <string, string > parameters = new Dictionary <string, string > ( )
            {
                { "client_name", "test"},
                { "redirect_uris", task.Result}
            };
            varpara = new FormUrlEncodedContent (parameters);
            start=para.ReadAsStringAsync().Result;

            string url="https://mstdn.jp/api/v1/apps?"+tt;


            string q = HttpUtility.UrlEncode(url);


            // Console.WriteLine("encoded q:"+q);

            // HTTP access
            Console.WriteLine(url);

            var req = WebRequest.Create(q);

            Console.WriteLine("bbbbb";

            req.Headers.Add("Accept-Language:ja,en-us;q=0.7,en;q=0.3");
            varres=req.GetResponse();

            // Convert Response (JSON) to Object
            ServiceResult info;
            using(res)
            {
                using(var resStream=res.GetResponseStream())
                {
                    varserializer = new DataContractJsonSerializer (typeof(ServiceResult)));
                    info=(ServiceResult) serializer.ReadObject(resStream);
                }
            }
            
            Console.ReadKey();
        }


        private static async void test()
        {
            Task.WaitAll(test_run());

        }

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

            test();



        }


        DataContract
        public class ServiceResult
        {

            DataMember
            public string id {get;set;}
            DataMember
            public string name {get;set;}

        }

c#

2022-11-12 15:08

1 Answers

UrlEncode causes an error
Maybe it's because the encoding has this string.

https%3a%2f%2fmstdn.jp%2fapi%2fv1%2fapps%3fclient_name%3dtest%26redirect_uris%3dhttps%253A%252F%252Fmstdn.jp%252Fauth%252Fsign_in

Unencoded

using System;
using System.Net;

public class program
{
    public static void Main()
    {
        WebRequest request = WebRequest.Create("https://mstdn.jp/api/v1/apps?client_name=test&redirect_uris=https%3A%2F%2Fmstdn.jp%2Fauth%2Fsign_in");
        Console.Write(request);
    }
}

https://dotnetfiddle.net/F54818

Encoded

using System;
using System.Net;
using System.Web;

public class program
{

    public static void Main()
    {
         US>string encoded_uri=HttpUtility.UrlEncode("https://mstdn.jp/api/v1/apps?client_name=test&redirect_uris=https%3A%2F%2Fmstdn.jp%2Fauth%2Fsign_in");
        WebRequest request = WebRequest.Create(encoded_uri);
        Console.Write(request);
    }
}

https://dotnetfiddle.net/F54818


2022-11-12 17:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.