2013-02-04 39 views
1

我試圖從Base API獲得響應,但我一直收到「500 Internal Server Error」錯誤。我想獲得至少401個HTTP響應,這意味着認證呼叫失敗。下面是使用基本API認證的描述:如何從Base CRM API身份驗證獲得響應

http://dev.futuresimple.com/api/authentication

這裏是我的代碼:

public string Authenticate() 
     { 
      string result = ""; 
      string url = "https://sales.futuresimple.com/api/v1/"; 
      string email = "[email protected]"; 
      string password = "pass"; 
      string postData = "email=" + email + "&password=" + password; 
      HttpWebRequest request = null; 
      Uri uri = new Uri(url + "authentication.xml"); 
      request = (HttpWebRequest)WebRequest.Create(uri); 
      request.Method = "POST"; 
      request.ContentType = "application/xml"; 
      request.ContentLength = postData.Length; 

      using (Stream writeStream = request.GetRequestStream()) 
      { 
       UTF8Encoding encoding = new UTF8Encoding(); 
       byte[] bytes = encoding.GetBytes(postData); 
       writeStream.Write(bytes, 0, bytes.Length); 
       writeStream.Close(); 
      } 

      try 
      { 
       using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
       { 
        using (Stream responseStream = response.GetResponseStream()) 
        { 
         using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8)) 
         { 
          result = readStream.ReadToEnd(); 
         } 
        } 
       } 
      } 
      catch (WebException ex) 
      { 
       ex = ex; 
      } 
      return result; 
     } 

回答

1

你設置ContentTypeapplication/xml - 這是請求主體的類型。您發送的正文(string postData = "email=" + email + "&password=" + password;)是表單編碼而不是xml。只要跳過request.ContentType = "application/xml";就行。或者,您可以將您的請求主體編碼爲xml。