Home > Net >  Can let the program using custom DNS address, rather than a system?
Can let the program using custom DNS address, rather than a system?

Time:05-17

Program using httpclient, but you need to specify the DNS to access to the destination address, I don't want to change the DNS system, can achieve exe program using your DNS Settings?

CodePudding user response:

What do you mean with specified DNS query IP first, and then use httpclient to access specific IP??

Well,
https://www.nuget.org/packages? Q=DNS
Here to find a you feel can be used, please take the nuget, his position as important as netcore itself, (you can have a problem, but also to all the problems, nature also will be someone to write the corresponding things, after all, this is not what commercial requirements, no money to do, we ought to try on nuget)

CodePudding user response:

I find DnsClient download first, and then gave a query API

https://dnsclient.michaco.net/
 var endpoint=new IPEndPoint (IPAddress. Parse (" 127.0.0.1 "), 8600);//here for your own DNS server specify 
Var client=new LookupClient (endpoint);

So that you can use this to specify DNS lookup query query, you can query the corresponding private DNS IP

Of course I need special instructions, obviously it is according to the thought and the way in the netcore, foreigners also specially shows you
 services. AddSingleton (the client); 

This way, you can like naming httpclient usage to use this stuff

CodePudding user response:

Because of blog park students inaction and deliberately IQ tax game, we can only to show how to developed in netcore
In order to not confused with what the core asp.net, I will not intentionally what asp.net play under the core (don't pull the netcore is what asp.net is always a core, what web)

The initial preparation work
1. New netcore console
2. Microsoft nuget reference. Extensions. Hosting
3. Continue to reference data DnsClient upstairs

The foundation design work
1. First of all, we can not do any specific implementation code, let's do some application design of global build, that the population in the field of architectural design, design

 static async Task Main (string [] args) 
{
IHostBuilder hostBuilder=new hostBuilder ();


HostBuilder. ConfigureServices (service=& gt;
{
Service. AddSingleton (new LookupClient (IPAddress. Parse (" 180.76.76.76 "), 53));//I use baidu public DNS to do test
Service. AddSingleton (a);
Service. AddSingletonService. AddHostedService});



Using (var host=await hostBuilder. StartAsync ())
{

Await the host WaitForShutdownAsync ();
}

}


Now let's concrete implementation design:
 public class private DnsHandler: HttpClientHandler 
{

Private readonly ILookupClient _lookupClient;


Public private DnsHandler (ILookupClient lookupClient)
{

_lookupClient=lookupClient;
}

Protected override async Task SendAsync (HttpRequestMessage request, CancellationToken CancellationToken)
{
Var host=request. RequestUri. Host;
Var ips=await _lookupClient. GetHostEntryAsync (host);//by defined above private DNS query IP

Var builder=new UriBuilder (request. RequestUri);
Builder. Host=ips. Expressions such as AddressList. First (). The ToString ();//modify original request host to your custom DNS query above the IP

Request. RequestUri=builder. Uri;//to reconstruct a new request uri

Return await base. SendAsync (request, cancellationToken);
}
}




Define a strongly typed httpclient to subsequent use and other httpclient apart
 public class private Dns address Httpclient 
{
Public HttpClient Client {get; }
Public private Dns address Httpclient (private DnsHandler DnsHandler)
{
The Client=new HttpClient (dnsHandler);//using the above definition of private DNS handler

}

}



 public class the main service entrance: IHostedService 
{
Private readonly private Dns address Httpclient _cutomdnsHttpclient;

The public the main service entrance (private Dns address Httpclient CutomdnsHttpclient)
{
_cutomdnsHttpclient=CutomdnsHttpclient;


}
Public async Task StartAsync (CancellationToken CancellationToken)
{
//I don't do what special things here, just after a custom DNS and access to the results, the main is to show how to use this thing

Var document=await _cutomdnsHttpclient. Client. GetStringAsync (" http://www.baidu.com ");

}

Public Task StopAsync (CancellationToken CancellationToken)
{
Return to Task.Com pletedTask;
}
}
  •  Tags:  
  • C#
  • Related