简体   繁体   中英

.NET: Why does this domain not resolve?

I am trying to programmatically test if a given domain name exists.

The following line of code works as expected:

IPHostEntry IPhst =Dns.GetHostEntry("google.com");

But it fails for some few domains on the very same host at the very same time. For example, the following throws an exception.

IPHostEntry IPhst =Dns.GetHostEntry("bks-campus.ch");

The resulting exception is identical to exceptions that are thrown when the domain name does not exist at all.

The weird thing is that the domain actually exists. I am able to browse it from the same machine that just ran the code above.

What might be the problem here and how can I work around this?

Edit: Dig also agrees that this domain name actually exists:

dig bks-campus.ch

; <<>> DiG 9.5.0-P2 <<>> bks-campus.ch
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 14744
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0

;; QUESTION SECTION:
;bks-campus.ch.                 IN      A

;; AUTHORITY SECTION:
bks-campus.ch.          3600    IN      SOA     dns1.bks-campus.ch. hostmaster.kanti-chur.ch. 1275546863 10800 3600 604800 86400

;; Query time: 1214 msec
;; SERVER: 192.168.0.1#53(192.168.0.1)
;; WHEN: Fri Jul  2 12:50:10 2010
;; MSG SIZE  rcvd: 94
; <<>> DiG 9.3.2 <<>> any bks-campus.ch @olympus
; (1 server found)
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1406
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:
;bks-campus.ch.                 IN      ANY

;; ANSWER SECTION:
bks-campus.ch.          86400   IN      SOA     dns1.bks-campus.ch. hostmaster.kanti-chur.ch. 1275546863 10800 3600 604800 86400
bks-campus.ch.          86400   IN      MX      20 cws02.netgrouper.ch.
bks-campus.ch.          86400   IN      MX      20 cws01.netgrouper.ch.
bks-campus.ch.          86400   IN      NS      dns1.bks-campus.ch.

;; AUTHORITY SECTION:
bks-campus.ch.          86400   IN      NS      dns1.bks-campus.ch.

;; ADDITIONAL SECTION:
cws02.netgrouper.ch.    32548   IN      A       194.150.160.32

;; Query time: 179 msec
;; SERVER: 192.168.2.4#53(192.168.2.4)
;; WHEN: Fri Jul 02 13:00:48 2010
;; MSG SIZE  rcvd: 193

There are no A (IPv4) records for bks-campus.ch , neither any CNAME (alias) or AAAA (IPv6). The response only tells us which servers handles mail (MX), dns (NS) and some administrative record (SOA). There's nothing in the response that tells the resolver where to find bks-campus.ch . Add an A record (or CNAME) and it will work.

google.com has an A record associating it with an IP address. bks-campus.ch does not have any A, AAAA or CNAME records, so you get an error.

Your browser is probably automatically adding the www when it can't find an A record for the domain.


Edit : Your dig output confirms that bks-campus.ch exists but has no A record.

bks-campus.ch doesn not have a DNS entry. Probably the browser translates to www.bks-campus.ch , which has a DNS entry, and would probably work from the code too.

EDIT: The domain name exists, but your DNS probably doesn't know it. Mine doesn't, but it knows www.bks-campus.ch .

What you are trying to do by your code is to see if the domain has a resolving dns A record for bks-campus.ch. What you want to do is request the SOA record for a domain. see this CodeProject project that searches for the MX record. Just change the type of record to SOA

This will only work if the domain has been delegated to a name server. There are some domains that have been registered and just not pointed to a DNS server. In these cases you will just get an error message if you enter the domain name into a browser.

The BEST way to find out if a domain has been registered is to do a WHOIS lookup. These are run by the TLD Registries to allow people to check who has registered a domain name. If you telnet to port 43 of whois.nic.coop and type in a domain name "nosuchdomain.coop" you will get the details of who registered that domain.

.coop registry WHOIS server

For help on using this server use "?" (without the quotes).
For more .coop information browse to http://www.coop

nosuchdomain.coop
No domain records were found to match "nosuchdomain.coop"

--------------------------------------------------------------------------------

The .coop registry WHOIS database provides information for .coop registered
names only. Although every effort is made to maintain the accuracy of the WHOIS
data, accuracy cannot be guaranteed.

This service is intended only for query-based access. You agree that you will
use this data only for lawful purposes and that, under no circumstances will you
use this data to: (a) allow, enable, or otherwise support the transmission by
e-mail, telephone, or facsimile of mass unsolicited, commercial advertising or
solicitations to entities other than the data recipient's own existing
customers; or (b) enable high volume, automated, electronic processes that send
queries or data to the systems of Registry Operator or any ICANN-Accredited
Registrar, except as reasonably necessary to register domain names or modify
existing registrations. The compilation, repackaging, dissemination or other use
of this Data is expressly prohibited without the prior written consent of
dotCoop. All rights reserved. dotCoop reserves the right to modify these terms
at any time. By submitting this query, you agree to abide by this policy.

BY USING THE WHOIS SERVICE AND THE DATA CONTAINED HEREIN OR IN ANY REPORT
GENERATED WITH RESPECT THERETO, IT IS ACCEPTED THAT DOTCOOP IS NOT LIABLE FOR
ANY DAMAGES OF ANY KIND ARISING OUT OF, OR IN CONNECTION WITH, THE REPORT OR THE
INFORMATION PROVIDED BY THE WHOIS SERVICE, NOR OMISSIONS OR MISSING INFORMATION.
THE RESULTS OF ANY WHOIS REPORT OR INFORMATION PROVIDED BY THE WHOIS SERVICE
CANNOT BE RELIED UPON IN CONTEMPLATION OF LEGAL PROCEEDINGS WITHOUT FURTHER
VERIFICATION, NOR DO SUCH RESULTS CONSTITUTE A LEGAL OPINION.

If you open a socket to this port on the correct server and just send them the domain, capture the response, and then parse this to see if the WHOIS server reports if the domain is registered or not.

Another way to see if a given domain exists is to do a NS lookup for the domain in question. If the domain exists, it must have a NS record, even if the registered nameservers for the domain time out.

Try setting the DNS servers for your NETWORK ADAPTER to 4.2.2.2 or 4.2.2.3 . Mostly If I'm not mistaken this would work because sometimes DNS servers of ISPs don't respond timely. Let me know your results in comments.

Lots of reasons spring to mind but the most likely is that your DNS infrastructure is not set up to resolve this domain.

Bear in mind that even if you can browse to it from a web browser, it doesn't tell you much if you are also going through a web proxy. It just tells you the proxy can resolve the host, not your local machine.

The easiest way is to try nslookup from the command line

nslookup bks-campus.ch

If this fails, the DNS your machine is using cannot resolve this, so there is no way your code can.

If this passes, then more investigation is required. Post back more info :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM