Java interface test using Youdao translation API

Source: Internet
Author: User
Tags md5 digest

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

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.