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!