Jump to content

Call API using C#


AnthonyV

Recommended Posts

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!

Link to comment
Share on other sites

  • 4 weeks later...

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated