AnthonyV Posted August 29, 2013 Share Posted August 29, 2013 Hi, How can i call the api webservice using c#? I cannot use uploadvalues or namevaluecollection because i'm using a silverlight application. This code and many others returns "result=error;message=Authentication Failed;" WebClient wHMCSclient = new WebClient(); string data = "{ username, ****}, { password, **** }, { action, getinvoices }"; WHMCSclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); var data= WHMCSclient.UploadString("https://domain/includes/api.php",data); Can someone please help me? Thanks! 0 Quote Link to comment Share on other sites More sharing options...
chrisdiphoorn Posted September 24, 2013 Share Posted September 24, 2013 The above code was almost right. Try this. public static string GetWHMCSAPIResults(string anaction) { // Instantiate the WebClient object WebClient WHMCSclient = new WebClient(); string username = "USERNAME"; string password = EncodeToMD5("PASSWD"); // Prepare a Name/Value collection to hold the post values NameValueCollection form = new NameValueCollection(); form.Add("username", username); form.Add("password", password); // the password will still need encoding is MD5 is a requirement form.Add("action", anaction); // action performed by the API:Functions form.Add("responsetype","xml"); // Post the data and read the response Byte[] responseData = WHMCSclient.UploadValues("http://URL/includes/api.php", form); // Decode and display the response. //nsole.WriteLine("\nResponse received was \n{0}", string result = System.Text.Encoding.ASCII.GetString(responseData); if (result.Contains("result=error")) { result = null ; } return result; } public static string EncodeToMD5(string input) { // step 1, calculate MD5 hash from input MD5 md5 = MD5.Create(); byte[] inputBytes = Encoding.ASCII.GetBytes(input); byte[] hash = md5.ComputeHash(inputBytes); // step 2, convert byte array to hex string StringBuilder sb = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { sb.Append(hash.ToString("X2")); } return sb.ToString(); } - - - Updated - - - Also make sure that the API user account has been setup, and that the IP addresses you are connecting to have been added to the setup. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.