DNS optimization knowledge collation for Android network interaction
Previous work, often encountered DNS resolution problems caused by network interaction operation is not normal.
In many mobile development processes, the URLs that interact with the server are usually included with the domain name. In the actual network interaction process, the first step will need to DNS resolution of the domain name.
In a complex network environment, DNS parsing can take a long time, or even fail to parse. This is often the case.
So the optimization of this step is very important.
There is a solution called: IP direct connection
is in the process of network interaction, skip DNS resolution of the domain name, directly with IP for network interaction. Can avoid this big trouble.
In response to this scheme, Httpdns has been derived from the following several famous:
- Dnspod d+ Mobile resolution service d+ Free
- Ali
- Sina
The basic implementation of these methods is:
- Send the domain name to the HTTPDNS server and get the IP address of the domain name
- Replace the IP address with the domain name for network access
Of course, there is a lot of detail in every step.
One important point is that for an IP host to mount multiple servers, the host header needs to set the domain name when using IP direct connection
The code is as follows:
public static HttpURLConnection gethttpurlconnection (String urlstring) throws ioexception { url url = new URL ( urlstring); String originhost = Url.gethost (); HttpURLConnection Conn; String Dstip = Httpdnsservice.getipbyhost (Url.gethost ()); Here gets the IP address if (dstip! = null) { LOG.D ("Httpdns Demo", "Get IP from Httpdns," + url.gethost () + ":" + dstip);
urlstring = Urlstring.replacefirst (Url.gethost (), dstip); url = new URL (urlstring); conn = (httpurlconnection) url.openconnection (); Set HTTP request header host domain conn.setrequestproperty ("host", originhost);//This is the setting of host return Conn in head; } else { log.d ("Httpdns Demo", "degrade to local DNS."); Return (HttpURLConnection) url.openconnection (); } }
For more content and introduction, you can refer to the following article:
- http://blog.csdn.net/a345017062/article/details/38047595
- The new idea of global accurate flow dispatching of "Goose Factory Network Affair"-httpdns Service explanation
- Https://github.com/CNSRE/HTTPDNSLib
DNS optimization knowledge collation for Android network interaction