Namecheap-dot-net
Namecheap-dot-net copied to clipboard
SetHosts method throws an exception
The following code throws an exception:
var api = new NameCheapApi("{username}", "{apiUser}", "{apiKey}", "{clientIp}");
DnsHostResult result = api.Dns.GetHosts("{sld}", "com");
api.Dns.SetHosts("{sld}", "com", result.HostEntries);
Exception details:
Message: Object reference not set to an instance of an object
StackTrace:
at NameCheap.Query.Execute(String command)
at NameCheap.DnsApi.SetHosts(String secondLevelDomain, String topLevelDomain, HostEntry[] hostEntries)
at TransactionRedirectChanger.Program.Main(String[] args) in C:\Users\derek.antrican\source\repos\TransactionRedirectChanger\TransactionRedirectChanger\Program.cs:line 30
Looks like this might be communicating incorrectly with the Namecheap API. After copying some of the code from the repo and debugging into it, this is the response coming back from Namecheap:
<ApiResponse Status="ERROR" xmlns="http://api.namecheap.com/xml.response">
<Errors>
<Error Number="5050900">Object reference not set to an instance of an object.</Error>
</Errors>
<Warnings />
<RequestedCommand>namecheap.domains.dns.sethosts</RequestedCommand>
<CommandResponse Type="namecheap.domains.dns.setHosts" />
<Server>PHX01APIEXT01</Server>
<GMTTimeDifference>--5:00</GMTTimeDifference>
<ExecutionTime>0.021</ExecutionTime>
</ApiResponse>
I have found the issue. One of my records was a URL redirect record which contained a "#" in the url (eg https://docs.google.com/spreadsheets/d/1BNaPNSkiD7bLXdpntabeWcdnZ7Vg5jMi3eUsEwBBKvc/edit#gid=1158913528). To solve this, I needed to URL Encode the parameter.
I would suggest that this be added to the Query.Execute()
method. For example:
foreach (KeyValuePair<string, string> param in _parameters)
url.Append("&").Append(param.Key).Append("=").Append(HttpUtility.UrlEncode(param.Value));