Android via HttpURLConnection and HttpClient networking proxy gateway settings

Source: Internet
Author: User

Android networking mainly using Httpurlconneciton and httpclient for networking, when mobile networking, we prefer WiFi network, followed by the choice of mobile networks, where the mobile network is mainly referred to Cmwap.

We all know that cmwap connections need to set proxy addresses and ports, so how do you set up proxies in the Android program? This is a problem.

HttpURLConnection Settings Agent

When we are using a mobile phone network in China, the following method can get the

10.0.0.172,80 port  
String host=android.net.proxy.getdefaulthost () directly;

the default proxy address int port =android.net.proxy.getdefaultport () can be obtained by andorid.net.Proxy and  
/or through Andorid.net.Proxy

To obtain the default proxy port  
socketaddress sa=new inetsocketaddress (host,port);  
Defines the proxy, where the proxy is derived from the java.net  
proxy proxy=new proxy (JAVA.NET.PROXY.TYPE.HTTP,SA);  
URL getUrl = new URL ("www.baidu.com");   
HttpURLConnection con = (httpurlconnection) geturl.openconnection

(proxy);//Set up proxy

HttpClient Settings Agent

Defaulthttpclient httpclient=new defaulthttpclient ();  
String host=proxy.getdefaulthost ()//Here the Proxy originates from the android.net  
int port = proxy.getport (context);/Ibid.  
Httphost Httphost = new Httphost (host, port);   
Set Agent  
Httpclient.getparams (). Setparameter

(connrouteparams.default_proxy,httphost);  
HttpGet httpget=new httppost ("<a 

href=" http://www.baidu.com ">www.baidu.com</a>")

;  
HttpResponse Response=httpclient.execute (HttpGet);

The first way: through the httpurlconnection to access

public static InputStream Gethttpurlconnectioninputstream (context context,string requesturl,map<string, String  
        > param) {URL url;  
        HttpURLConnection conn = null;  
        InputStream input = null;  
            try {url = new URL (requesturl); if (Getapntype (context) ==networkutil.cmwap)//When the requested network is WAP, you need to add China Mobile Agent {Proxy proxy = n  
                EW Proxy (java.net.proxy.type.http,new inetsocketaddress ("10.0.0.172", 80));  
            conn = (httpurlconnection) url.openconnection (proxy);  
              }else{conn = Url.openconnection ();    } conn.setconnecttimeout (10000);  Request Timeout Conn.setrequestmethod ("POST");   Request Mode Conn.setreadtimeout (1000);  
                Read Timeout conn.setdooutput (true);  
                Conn.setdoinput (TRUE);  
                Conn.setusecaches (FALSE); Conn. Setrequestproperty ("Charset", "UTF-8");  
                Conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");      
                OutputStream OS = Conn.getoutputstream ();  
                StringBuilder sb = new StringBuilder ();  
                Iterator<string> it = Param.keyset (). iterator ();  
                    while (It.hasnext ()) {String key = It.next ();  
                    String value = Param.get (key);  
                Sb.append (Key). Append ("="). Append (Value). Append ("&");  
                String p = sb.tostring (). substring (0, Sb.length ()-1);  
                SYSTEM.OUT.PRINTLN ("requested parameter" +p);  
                Os.write (P.getbytes ("Utf-8"));  
                Os.close ();  
                if (conn!=null) {input = Conn.getinputstream ();  
        } catch (Exception e) {e.printstacktrace ();  
 }       return input; }

The above way is httpurlconnection, this way in the development of Android is also more commonly used, I hope friends should also be familiar with the master!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.