Write interface test framework, find Youdao translation API to test data
Package Com.httpgettest;import Java.beans.encoder;import Java.net.urlencoder;import java.security.MessageDigest; Import Java.util.hashmap;import Java.util.map;public class Creathttpurl {public static void main (string[] args) { System.out.print (Creaturl ("Teacher", "en", "zh-chs")); static string Appkeyid = "7743eee7f7e11d75"; static string AppKey = "Bwpochuflsuty4ngvyjoeunn9gbqduzl";/** * Generate Youdao URL * @pa RAM to search for words * @param source language * @param target language * @return */public static string Creaturl (String query,string from,string to) {string Salt = string.valueof (System.currenttimemillis ()); String sign = MD5 (Appkeyid + query + salt + appKey); map<string, string> params = new hashmap<string,string> ();p arams.put ("q", query);p arams.put ("from", from) ;p arams.put ("to");p arams.put (' sign ', sign);p arams.put ("salt", salt);p arams.put ("AppKey", Appkeyid); String urlstr = "Https://openapi.youdao.com/api"; return geturlwithquerystring (URLSTR, params);} public static string Createparam (String query,string from,stRing to) {String salt = string.valueof (System.currenttimemillis ()); String sign = MD5 (Appkeyid + query + salt + appKey); map<string, string> params = new hashmap<string,string> ();p arams.put ("q", query);p arams.put ("from", from) ;p arams.put ("to");p arams.put (' sign ', sign);p arams.put ("salt", salt);p arams.put ("AppKey", Appkeyid); String urlstr = ""; return geturlwithquerystring (URLSTR, params);} /** * Generate 32-bit MD5 summary * @param str * @return */public static string MD5 (string string) {if (string = = null) {return null;} Char hexdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};byte [] btinput = String.getbytes (); try {/** obtains MD5 Digest algorithm MessageDigest object **/messagedigest mdinst = messagedigest.getinstance ("MD5");/** Updates the digest **/mdinst.update (btinput) with the specified byte,/** obtains ciphertext **/byte []MD = Mdinst.digest ();/** converts ciphertext into 16-binary string form **/int j = md.length;c Har str[] = new Char[j*2];int k = 0;for (byte byte0:md) {str[k++] = hexdigits[byte0 >>> 4 & 0xf];str[k++] = h Exdigits[byte0 & 0XF];} return new String (str); catch (Exception e) {e.printstacktrace (); return null;}} public static string Geturlwithquerystring (String url,map<string, String>params) {if (params = = null) {return URL;} StringBuilder builder = new StringBuilder (URL); if (Url.contains ("?")) {Builder.append ("&");} else {builder.append ("?");} int i = 0; For (String Key:params.keySet ()) {String value = Params.get (key); if (value = = null) {//filter empty keycontinue;} if (i! = 0) {builder.append (' & ');} Builder.append (key); Builder.append (' = '); Builder.append (encode (value)); i++;} return builder.tostring (); }/** * URL encoded * @param input * @return */public static String encode (String input) {if (input = = NULL {return "";} try {return Urlencoder.encode (input, "Utf-8"),} catch (Exception e) {e.printstacktrace ();} return input;}}
A link to: HTTP://AI.YOUDAO.COM/DOCS/API.S
You can test yourself according to the interface protocol that Youdao translates
Java interface test using Youdao translation API