System.Net.WebException:远程名称无法解析: [英] System.Net.WebException: The remote name could not be resolved:

查看:3207
本文介绍了System.Net.WebException:远程名称无法解析:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试的终端,我遇到一些问题。

I am testing an endpoint that i am experience some issues with.

我只是使用的HttpClient 在一个循环,每一个小时做的请求。

I am simply using HttpClient in a loop that each hour do a request.

var httpClient = new HttpClient();
var message = httpClient.GetAsync(url).Result;
Console.WriteLine(message.StatusCode);

在一段时间我得到这个例外。

Once a while I am getting this exception.

System.Net.Http.Htt prequestException:occurreð错误,同时发送   请求。 ---> System.Net.WebException:远程名称不能   解决:XXX

System.Net.Http.HttpRequestException: An error occurre d while sending the request. ---> System.Net.WebException: The remote name could not be resolved: 'xxx'

的经验是,可被访问的网址的异常后的权利。在浏览器中它仅仅是为了刷新页面,一切都很好。

The experience is that right after the exception the url can be accessed. In a browser its simply to refresh the page and all is good.

我仍然还没有接到用户报告遇到它,所以我如果它只是一个局部问题,想在这里,但可以使用一些信息,以帮助诊断。

I still havent got any reports from users experience it so I am wondering if its just a local issue here but could use a little information to help diagnose.

有没有一种方法来检查的远程名称无法解析的是由一个DNS问题或网络服务器问题从例外。我可以得到更多的信息出的HttpClient 或者我需要更先进的诊断工具

Is there a way to check if The remote name could not be resolved is caused by an DNS issue or by a WebServer Issue from the exceptions. Can i get more information out of HttpCLient or do i need more advanced diagnostic tools

推荐答案

这是的也许的由本地网络连接问题造成的(也是一个DNS错误是可能的)。

It's probably caused by a local network connectivity issue (but also a DNS error is possible).

这可能发生,没有什么可以做。

It may happen, there isn't much you can do.

我会建议总是用那只(网络相关)code在与尝试 / 块(因为还建议这里其他的犯错误的的操作)。处理已知的例外,等待一小(比如1000毫秒),然后再试一次(比如为3次)。只有失败的时刻,你可以退出/你的用户报告错误。非常原始的例子是这样的:

What I'd suggest to always wrap that (network related) code in a loop with a try/catch block (as also suggested here for other fallible operations). Handle known exceptions, wait a little (say 1000 msec) and try again (for say 3 times). Only if failed all times then you can quit/report an error to your users. Very raw example like this:

private const int NumberOfRetries = 3;
private const int DelayOnRetry = 1000;

public static HttpResponseMessage GetFromUrl(string url) {
    for (int i=1; i <= NumberOfRetries; ++i) {
        try {
            // Also consider to make this method async and await this
            return new HttpClient().GetAsync(url).Result;
        }
        catch (Exception e) {
            // DO BETTER THAN THIS! Catch what you want to handle,
            // not all exceptions worth a retry. Documentation and many
            // tests will help you to narrow a limited subset of
            // exceptions and error codes.

            // Last one, (re)throw exception and exit
            if (i == NumberOfRetries)
                throw;

            // Many network related errors will recover "automatically"
            // after some time, exact delay is pretty arbitrary and
            // should be determined with some tests. 1 second is pretty
            // "good" for local I/O operations but network issues may
            // need longer delays.
            Thread.Sleep(DelayOnRetry);
        }
    }
}

这篇关于System.Net.WebException:远程名称无法解析:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆