花費小時數試圖找出這一個。響應消息始終有一個404未找到的錯誤。任何幫助將不勝感激!C#HttpClient返回錯誤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
更新:我意識到了錯誤。註冊網址不正確(錯過了一個字符)。
您可能想查看[Fiddler](http://www.telerik.com/fiddler),從這種調試東西中獲取猜測。 –
謝謝@Aydin Adn。我一定會試一試。 –