zoukankan      html  css  js  c++  java
  • java HttpClient GET请求

     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();
    	
    	@Override
    	public 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"
    }
    
  • 相关阅读:
    postgres 流复制集群配置(一)
    postgres 文件系统级别的备份 pg_dump
    postgres 基于基础备份的恢复操作 (二)
    postgres 基于基础备份的恢复操作 之恢复文件详解recover.conf(一)
    postgres 基础备份-->pg_basebackup (二)
    postgres 基础备份-->pg_start_ backup与pg_stop_ backup (一)
    postgres WAL归档
    postgres 并行扫描--索引的正确打开方式
    Python标准库
    awesome python 中文版 相见恨晚!
  • 原文地址:https://www.cnblogs.com/dreamyu/p/6876116.html
Copyright © 2011-2022 走看看