HttpClient GET request Small instance, first simple record.
Package Com.httpclientget;import Java.io.ioexception;import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.httpclients;import Org.apache.http.util.entityutils;public class TestGet {public static void Main (string[] args) {new Get (). Start ();}} Class Get extends Thread{httpclient client = Httpclients.createdefault (), @Overridepublic Void Run () {String urlstr = "http S://openapi.youdao.com/api?q=teacher&salt=1495119694612&sign=cb78bfcea15c3ab13a1bb015efcbdf0a&from =en&appkey=7743eee7f7e11d75&to=zh-chs "; HttpGet get = new HttpGet (URLSTR); try {httpresponse response = Client.execute (get); httpentity entity = response.getentity (); String result = entityutils.tostring (entity, "UTF-8"); System.out.print (result);;} catch (Clientprotocolexception e) {e.printstacktrace ();} catch (IOException e) {e.Printstacktrace ();}}}
Run Result:
{"Tspeakurl": "Https://dict.youdao.com/dictvoice?audio=%E8%80%81%E5%B8%88&le=auto&channel= 7743eee7f7e11d75&rate=4 "," Web ": [{" Value ": [" Teacher "," teacher ", "Teacher"], "key": "Teacher"}, {"Value": ["Headmaster", "Principal", "Senior Teacher"], "key": "Head Teacher"}, {"Value" : ["Substitute teacher", "substitute West seat", "substitute"], "key": "Probation Teacher" }], "Query": "Teacher", "translation": ["Teacher"], "ErrorCode": "0", "basic": {"us- Phonetic ":" ' Tit?? "," Phonetic ":" ' ti?t?? "," uk-phonetic ":" ' ti?t?? "," explains ": [" N. Teacher Teacher "," N. (Teacher) name; (English) Beecher "]}," Speakurl ":" Https://dict.youdao.com/dictvoice?audio=teacher& ; Le=auto&channel=7743eee7f7e11d75&raTe=4 "}
Java HttpClient GET request