2016-03-05 181 views
1

花費小時數試圖找出這一個。響應消息始終有一個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

更新:我意識到了錯誤。註冊網址不正確(錯過了一個字符)。

+0

您可能想查看[Fiddler](http://www.telerik.com/fiddler),從這種調試東西中獲取猜測。 –

+0

謝謝@Aydin Adn。我一定會試一試。 –

回答

1

我覺得自己的基本網址是越來越重複:

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

你指定backendlessBase兩次。如果您已使用BaseAddress指定基數,請不要在呼叫中再次指定它。

另外,如果您仔細閱讀文檔,它是users/register,而不是user/register

+0

我從PostAsync調用中刪除了backendlessBase。它現在有signupUrl。仍然返回相同的錯誤。 –

+0

是的,我剛纔意識到。感謝您指出,@Matti Virkkunen。 –

相關問題