java HttpClient GET請求

來源:互聯網
上載者:User

標籤:class   code   ntp   tsp   str   over   protocol   java   rom   

 HttpClient GET請求小執行個體,先簡單記錄下。

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 = "https://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();}}}

 運行結果:

{    "tSpeakUrl": "https://dict.youdao.com/dictvoice?audio=%E8%80%81%E5%B8%88&le=auto&channel=7743eee7f7e11d75&rate=4",    "web": [        {            "value": [                "教師",                "老師",                "教師"            ],            "key": "Teacher"        },        {            "value": [                "校長",                "校長",                "進階教師"            ],            "key": "Head teacher"        },        {            "value": [                "代課教師",                "代課西席",                "代課"            ],            "key": "probation teacher"        }    ],    "query": "teacher",    "translation": [        "老師"    ],    "errorCode": "0",    "basic": {        "us-phonetic": "‘tit??",        "phonetic": "‘ti?t??",        "uk-phonetic": "‘ti?t??",        "explains": [            "n. 教師;導師",            "n. (Teacher)人名;(英)蒂徹"        ]    },    "speakUrl": "https://dict.youdao.com/dictvoice?audio=teacher&le=auto&channel=7743eee7f7e11d75&rate=4"}

 

java HttpClient GET請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.