无法解析远程名称:' testapi.internet.bs'

时间:2015-03-19 11:57:29

标签: c# asp.net asp.net-web-api

如何获得域名可用性?即时通讯使用此代码,但收到错误说 - " The    无法解析远程名称:' testapi.internet.bs'" 并且-url没有得到任何输出  https://testapi.internet.bs/Domain/Check?ApiKey=test123&Password=test123&Domain=google.com

 public StreamReader URLServerRequest(string url)
                    {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    StreamReader stream = new StreamReader(request.GetResponse().GetResponseStream());
                    return stream;
                    }

1 个答案:

答案 0 :(得分:1)

您可以使用下面的示例,但它并不完美,但演示了从响应中读取status值所需执行的操作。您应该检查API是否支持返回结构化响应,例如JSON数据。

static void Main(string[] args)
{
    string domainAvailability = GetDomainAvailability("https://testapi.internet.bs/Domain/Check?ApiKey=testapi&Password=testpass&Domain=google.com");
    Console.WriteLine(domainAvailability);
    Console.Read();
}

public static string GetDomainAvailability(string url)
{
    string content = string.Empty;
    using (WebClient wc = new WebClient())
    {
        content = wc.DownloadString(url);
    }

    string[] responseValues = null;
    if (!string.IsNullOrEmpty(content))
    {
        responseValues = content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
    }
    else
    {
        return "empty response";
    }

    if (responseValues.Any(x => x.Contains("status=")))
    {
        return responseValues.FirstOrDefault(x => x.Contains("status=")).Split('=')[1];
    }
    else
    {
        return "status parameter not found";
    }
}

这会为您的示例

返回UNAVAILABLE