C# HttpClient returns 错误 404 未找到

C# HttpClient returns error 404 Not found

花了几个小时试图解决这个问题。响应消息总是有 404 未找到错误。任何帮助将不胜感激!

public static class BackendlessAPIHelper
{
    internal static string appId = "my-app-id";
    internal static string restSecret = "my-api-secret";
    internal static string backendlessBase = "https://api.backendless.com/";
    internal static string signupUrl = "v1/user/register";

    public static async Task<bool> UserSignup(string username, string password)
    {
        bool signupsuccessful = false;
        var client = new HttpClient();
        UserSignupBackendless newuser = new UserSignupBackendless { email = username, password = password };
        string newuserjson = JsonConvert.SerializeObject(newuser);

        try
        {
            client.BaseAddress = new Uri(backendlessBase);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("application-id", BackendlessAPIHelper.appId);
            client.DefaultRequestHeaders.Add("secret-key", BackendlessAPIHelper.restSecret);
            client.DefaultRequestHeaders.Add("application-type", "REST");
            StringContent theContent = new StringContent(newuserjson, System.Text.Encoding.UTF8, "application/json");

            HttpResponseMessage response = await client.PostAsync(backendlessBase+signupUrl, theContent);
            if (response.IsSuccessStatusCode)
            {
                signupsuccessful = true;
                return signupsuccessful;
            }
            else
                return signupsuccessful;
        }
        catch(Exception ex)
        {
            ex.ToString();
            return signupsuccessful;
        }
    }
}

这是 API 文档:https://backendless.com/documentation/users/rest/users_user_registration.htm

我关注了位于此处的文章:http://blogs.msdn.com/b/wsdevsol/archive/2013/02/05/how-to-use-httpclient-to-post-json-data.aspx

更新:我意识到错误了。注册 URL 不正确(遗漏了一个字符)。

我认为您的基础 URL 重复了:

client.BaseAddress = new Uri(backendlessBase);
// ...
HttpResponseMessage response = await client.PostAsync(backendlessBase+signupUrl, theContent);

您指定了 backendlessBase 两次。如果您已经使用 BaseAddress 指定了基数,请不要在调用中再次指定它。

此外,如果您仔细阅读文档,它是 users/register,而不是 user/register