zoukankan      html  css  js  c++  java
  • 天气接口 乱码问题

    请求天气接口(http://wthrcdn.etouch.cn/weather_mini?citykey=101210101) 乱码问题

    public static String  sendGet2(String url) throws IOException {
                //实例化httpclient,(4.5新版本和以前不同),实例化方式有两种
                CloseableHttpClient httpClient = HttpClients.createDefault();
                HttpGet httpGet = new HttpGet(url);
                httpGet.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Firefox/60.0");
                CloseableHttpResponse response = null;
                try {
                    /**
                     * 底层http链接仍由响应对象保存
                     * 允许直接从网络套接字流式传输响应内容
                     * 为了确保正确释放系统资源
                     * 用户必须从finally子句中调用CloseableHttpResponse #close()
                     */
                    response = httpClient.execute(httpGet);
                    System.out.println(response.getStatusLine());
                    HttpEntity entity = response.getEntity();
                    //对响应主体做一些有用的事情
                    //并确保他完全被消耗掉
                    String s = EntityUtils.toString(entity, "utf-8");
                    return s;
                } catch (IOException e) {
                    e.printStackTrace();
                    return e.toString();
                } finally {
                    response.close();
                }
        }

    即可解决该问题。

  • 相关阅读:
    js MD5加密后的字符串
    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    归并排序
    C#分解质因数
    C#找出第n到m个素数之间所有之和
    C#打印0到100的素数
    for循环练习
    express总结(一)
    Nodejs总结(一)
    Webpack配置及使用
  • 原文地址:https://www.cnblogs.com/mikemhm/p/12050421.html
Copyright © 2011-2022 走看看