- Import Java.io.ByteArrayOutputStream;
- Import Java.io.InputStream;
- Import java.io.UnsupportedEncodingException;
- Import Java.net.URLEncoder;
- Import java.util.ArrayList;
- Import Java.util.LinkedHashMap;
- Import java.util.List;
- Import Java.util.Map;
- Import Java.util.Map.Entry;
- Import Java.util.TreeMap;
- Requires Httpclient-x.x.jar,httpcore-x.x.jar,commons-logging-x.x.jar,commons-httpclient-x.x.jar,commons-codec-x.x.jar packet to send HTTP request
- Import org.apache.commons.httpclient.URIException;
- Import Org.apache.commons.httpclient.util.URIUtil;
- Import org.apache.http.HttpEntity;
- Import Org.apache.http.HttpResponse;
- Import Org.apache.http.NameValuePair;
- Import org.apache.http.client.HttpClient;
- Import org.apache.http.client.entity.UrlEncodedFormEntity;
- Import Org.apache.http.client.methods.HttpGet;
- Import Org.apache.http.client.methods.HttpPost;
- Import org.apache.http.impl.client.DefaultHttpClient;
- Import Org.apache.http.message.BasicNameValuePair;
- public class Sntest {
- public static void Main (string[] args) throws Exception {
- Sntest sntest = new Sntest ();
- Sntest.testget ();
- Sntest.testpost ();
- }
- public void Testget () throws Exception {
- /**
- * Take http://api.map.baidu.com/geocoder/v2/?address= Baidu mansion &output=json&ak=yourak as an example
- * AK Set SN Check cannot be used directly must be appended to the URL at the end of the SN value, GET request calculation sn and URL parameters in the order of occurrence, need to be ordered to populate the Paramsmap,
- * The POST request is populated alphabetically, with reference to Testpost ()
- */
- Map Paramsmap = new linkedhashmap<string, string> ();
- Paramsmap.put ("Address", "Baidu Building");
- Paramsmap.put ("Output", "json");
- Paramsmap.put ("AK", "Yourak");
- Call the following toquerystring method to encode all the value in Paramsmap UTF8
- String paramsstr = toquerystring (Paramsmap);
- Paramsstr front stitching on/geocoder/v2/?, behind the direct stitching Yoursk
- String wholestr = new String ("/geocoder/v2/?" + paramsstr + "Yoursk");
- UTF8 code for the above WHOLESTR
- String TempStr = Urlencoder.encode (Wholestr, "UTF-8");
- Call the following MD5 method to get the SN signature value
- String sn = MD5 (TEMPSTR);
- Send GET request after SN
- HttpClient client = new Defaulthttpclient ();
- HttpGet httpget = new HttpGet (
- "Http://api.map.baidu.com/geocoder/v2/?address= Baidu Building &output=json&ak=yourak&sn="
- + sn);
- HttpResponse response = Client.execute (HttpGet);
- InputStream is = Response.getentity (). getcontent ();
- String result = Instream2string (IS);
- Print response content
- SYSTEM.OUT.PRINTLN (result);
- }
- public void Testpost () throws Exception {
- /**
- * Take http://api.map.baidu.com/geodata/v3/geotable/create CREATE TABLE as an example
- */
- linkedhashmap<string, string> paramsmap = new linkedhashmap<string, string> ();
- Paramsmap.put ("Geotype", "1");
- Paramsmap.put ("AK", "Yourak");
- Paramsmap.put ("name", "Geotable80");
- Paramsmap.put ("is_published", "1");
- The POST request is populated alphabetically by alphabetical order of the above Paramsmap by key
- map<string, string> treeMap = new treemap<string, string> (PARAMSMAP);
- String paramsstr = toquerystring (TREEMAP);
- String wholestr = new String ("/geodata/v3/geotable/create?" + paramsstr
- + "Yoursk");
- String TempStr = Urlencoder.encode (Wholestr, "UTF-8");
- Call the following MD5 method to get the SN signature value
- String sn = MD5 (TEMPSTR);
- HttpClient client = new Defaulthttpclient ();
- HttpPost post = new HttpPost (
- "Http://api.map.baidu.com/geodata/v3/geotable/create");
- list<namevaluepair> params = new arraylist<namevaluepair> ();
- Params.add (New Basicnamevaluepair ("Geotype", "1"));
- Params.add (New Basicnamevaluepair ("AK", "Yourak"));
- Params.add (New Basicnamevaluepair ("name", "Geotable80"));
- Params.add (New Basicnamevaluepair ("is_published", "1"));
- Params.add (New Basicnamevaluepair ("SN", SN));
- Httpentity formentity = new urlencodedformentity (params);
- Post.setentity (formentity);
- HttpResponse response = Client.execute (POST);
- InputStream is = Response.getentity (). getcontent ();
- String result = Instream2string (IS);
- Print response content
- SYSTEM.OUT.PRINTLN (result);
- }
- UTF8 encode all the value in map, stitching back the result
- Public String toquerystring (map<?,? > Data)
- Throws Unsupportedencodingexception, Uriexception {
- StringBuffer queryString = new StringBuffer ();
- For (entry<?,? > Pair:data.entrySet ()) {
- Querystring.append (Pair.getkey () + "=");
- Querystring.append (Urlencoder.encode (String) Pair.getvalue (),
- "UTF-8") + "&");
- Querystring.append (Uriutil.encodequery (String) Pair.getvalue (),
- "UTF-8") + "&");
- }
- if (querystring.length () > 0) {
- Querystring.deletecharat (Querystring.length ()-1);
- }
- return querystring.tostring ();
- }
- MD5 calculation method, call the MessageDigest library function, and convert the result of byte array to 16 binary
- public string MD5 (string md5) {
- try {
- Java.security.MessageDigest MD = Java.security.MessageDigest
- . getinstance ("MD5");
- byte[] Array = md.digest (Md5.getbytes ());
- StringBuffer sb = new StringBuffer ();
- for (int i = 0; i < Array.Length; ++i) {
- Sb.append (integer.tohexstring (array[i & 0xFF) | 0x100)
- . substring (1, 3));
- }
- return sb.tostring ();
- } catch (Java.security.NoSuchAlgorithmException e) {
- }
- return null;
- }
- Convert input flow to string
- private static String instream2string (InputStream is) throws Exception {
- Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
- byte[] buf = new byte[1024];
- int len =-1;
- while (len = Is.read (BUF))! =-1) {
- Baos.write (buf, 0, Len);
- }
- return new String (Baos.tobytearray (), "UTF-8");
- }
- }
Shows how the GET, POST request is SN, and how SN is used