Creating an Account for .NET
To create an account using the UserAcctHierarchyService wsdl, you will need to create a C# Console Application project with a Web Reference that contains an override function which specifies that requests are to use "Basic" HTTPS authentication.
Creating an account for .NET
1. | In Visual Studio 2008 or later, create a new C# Console Application project. |
2. | In the Solution Explorer, right-click the project icon and select Add Service Reference. |
3. | Click Advanced at the bottom of the Add Service screen and then Add Web Reference at the bottom of the Service Reference Settings screen. |
4. | Enter |
5. | Change the Web reference name to UserAcctOrdSvc and click Add Reference. The Solution Explorer view should have a new UserAcctOrdSvc icon in the Web References folder. |
6. | Double-click the UserAcctOrdSvc icon, which opens the Object Browser. |
7. | In the Object Browser, expand WebServiceTest.UserAcctOrdSvc, and double-click one of the items in the tree. This should open the proxy class source file Reference.cs. |
8. | Near the top of Reference.cs, find the line that begins: |
public v3UserAcctHierarchyService()
and insert the following function directly above:
protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest request;
request = (HttpWebRequest)base.GetWebRequest(uri);
if (PreAuthenticate)
{
NetworkCredential networkCredentials =
Credentials.GetCredential(uri, "Basic");
if (networkCredentials != null)
{
byte[] credentialBuffer =
new
System.Text.UTF8Encoding( ).GetBytes(
networkCredentials.UserName + ":" +
networkCredentials.Password);
request.Headers["Authorization"] =
"Basic " +
Convert.ToBase64String(credentialBuffer);
}
else
{
throw new ApplicationException(
"No network credentials");
}
}
return request;
}
9. | At the top of the file in the namespace function for the project, add the following: |
using System.Net;
10. | In the Program.cs file, add a Web Service request to create an account. For example: |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using WebServiceTest.UserAcctOrdSvc;
namespace WebServiceTest
{
class Program
{
static void Main(string[] args)
{
v3UserAcctHierarchyService usrAccSer = new
v3UserAcctHierarchyService();
// begin copied-and-pasted authorization code
NetworkCredential netCredential =
new NetworkCredential("username@company.com", "MyPassword");
Uri uri = new Uri(usrAccSer.Url);
ICredentials credentials = netCredential.GetCredential(uri, "Basic");
usrAccSer.Credentials = credentials;
usrAccSer.PreAuthenticate = true;
Console.WriteLine("[Successfully authenticated...]");
// end copied-and-pasted authorization code
// one and only account
createAcctRequestType acc1 = new createAcctRequestType();
accountDataType[] account = new accountDataType[1];
acc1.account = account;
account[0] = new accountDataType();
// make sure to change the Account ID for each test...
account[0].id = "ExampleAccountID";
account[0].name = "ExampleAccountName";
account[0].description = "ExampleDescriptionForAccount";
// add address for the respective account
addressDataType address = new addressDataType();
address.address1 = "No.45, 2nd Street";
address.address2 = "Sunset Avenue";
address.city = "Bangalore";
address.state = "Karnataka";
address.zipcode = "560054";
address.country = "IN";
address.region = "Asia";
AcctType accountType = new AcctType();
accountType = AcctType.CUSTOMER;
// this function is in the auto-generated Reference.cs code
createAcctResponseType creatAccResType = usrAccSer.createAccount(acc1);
if (creatAccResType.statusInfo.status != StatusType.SUCCESS)
{
Console.WriteLine("Failed to create account: " +
creatAccResType.statusInfo.reason);
}
else
{
Console.WriteLine("Success!");
}
}
}
}
11. | Build and run the application. |