Condividi tramite


Check DNS Prefix Availability

 

The Check DNS Prefix Availability operation checks whether the specified DNS prefix is available for creating a profile.

Request

The Check DNS Prefix Availability request is specified as follows. Replace <subscription-id> with the subscription ID. Replace <dns-name> with the DNS name that you want to use. You must include .trafficmanager.net in the name.

Method

Request URI

GET

https://management.core.windows.net/<subscription-id>/services/WATM/operations/isavailable/<dns-name>

You must make sure that the request that is made to the management service is secure. For additional details, see Authenticating Service Management Requests.

URI Parameters

None.

Request Headers

The following table describes the request headers.

Request Header

Description

x-ms-version

Required. Specifies the version of the operation to use for this request. This header should be set to 2011-10-01 or higher. For more information about versioning headers, see Service Management Versioning.

Request Body

None.

Response

The response includes an HTTP status code, a set of response headers, and a response body.

Status Code

A successful operation returns status code 200 (OK). For information about status codes, see Service Management Status and Error Codes.

Response Headers

The response for this operation includes the following headers. The response may also include additional standard HTTP headers. All standard headers conform to the HTTP/1.1 protocol specification.

Response Header

Description

x-ms-request-id

A value that uniquely identifies a request made against the management service.

Response Body

The format of the request body is as follows:

<?xml version="1.0" encoding="iso-8859-1"?>
<AvailabilityResponse xmlns="https://schemas.microsoft.com/windowsazure">
  <Result>availability-of-name</Result>
</AvailabilityResponse>

The following table describes the elements in the response body.

Element name

Description

Result

Indicates whether the DNS name is available for you to use.

Possible values are:

  • true

  • false

Remarks

After you have verified the availability of the DNS name, you can create a profile using Create Profile. For more information about Azure Traffic Manager, see Azure Traffic Manager.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Net;

namespace CheckDNSAvailability
{
  class Program
  {
    private const string Thumbprint = "certificate-thumbprint";
    private const string SubscriptionId = "subscription-id";
    private const string Version = "2011-10-01";
    private const string DNSname = "dns-name.trafficmanager.net";

    // Gets or sets the certificate that matches the Thumbprint value.
    private static X509Certificate2 Certificate { get; set; }

    static void Main(string[] args)
    {
      Certificate = GetStoreCertificate(Thumbprint);
      
      // Create the uri and submit the request
      string uriFormat = "https://management.core.windows.net/{0}/services/WATM/operations/isavailable/{1}";
      Uri uri = new Uri(String.Format(uriFormat, SubscriptionId, DNSname));
      XDocument responseBody;
      HttpWebResponse response = InvokeRequest(uri, "GET", out responseBody);

      // Get the response
      HttpStatusCode statusCode = statusCode = response.StatusCode;
      Console.WriteLine("The status of the operation: {0}\n\n", statusCode.ToString());
      Console.WriteLine(responseBody.ToString(SaveOptions.OmitDuplicateNamespaces));
      Console.Write("Press any key to continue:");
      Console.ReadKey();
    }

    private static HttpWebResponse InvokeRequest(
      Uri uri,
      string method,
      out XDocument responseBody)
    {
      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
      request.Method = method;
      request.Headers.Add("x-ms-version", Version);
      request.ClientCertificates.Add(Certificate);
      request.ContentType = "application/xml";

      responseBody = null;
      HttpWebResponse response;

      try
      {
        response = (HttpWebResponse)request.GetResponse();
      }
      catch (WebException ex)
      {
        response = (HttpWebResponse)ex.Response;
      }

      XmlReaderSettings settings = new XmlReaderSettings();
      settings.DtdProcessing = DtdProcessing.Ignore;

      if (response.ContentLength > 0)
      {
        using (XmlReader reader = XmlReader.Create(response.GetResponseStream(), settings))
        {
          try
          {
            responseBody = XDocument.Load(reader);
          }
          catch
          {
            responseBody = null;
          }
        }
      }
      response.Close();
      return response;
    }

    private static X509Certificate2 GetStoreCertificate(string thumbprint)
    {
      List<StoreLocation> locations = new List<StoreLocation>
      { 
        StoreLocation.CurrentUser, 
        StoreLocation.LocalMachine
      };

      foreach (var location in locations)
      {
        X509Store store = new X509Store("My", location);
        try
        {
          store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
          X509Certificate2Collection certificates = store.Certificates.Find(
          X509FindType.FindByThumbprint, thumbprint, false);
          if (certificates.Count == 1)
          {
            return certificates[0];
          }
        }
        finally
        {
          store.Close();
        }
      }
      throw new ArgumentException(string.Format(
        "A Certificate with Thumbprint '{0}' could not be located.",
        thumbprint));
    }
  }
}