xuesongya / study

study Angular2 and NodeJS
1 stars 0 forks source link

HttpClient #7

Open xuesongya opened 7 years ago

xuesongya commented 7 years ago

http://blog.csdn.net/wangpeng047/article/details/19624529/

xuesongya commented 7 years ago

:heavy_check_mark: 示例

        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(
                "http://127.0.0.1:8999/bes/u-route/oct-rest/gadget/search");

        List<NameValuePair> formparams = new ArrayList<>();
        formparams.add(new BasicNameValuePair("params", "{condition:''}"));

        UrlEncodedFormEntity formEntity;
        try
        {
            formEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            post.setEntity(formEntity);
            post.setHeader("content-type", "application/x-www-form-urlencoded");
            post.setHeader("cache-control", "no-cache");
            CloseableHttpResponse response = (CloseableHttpResponse) client.execute(post);
            try
            {
                HttpEntity entity = response.getEntity();
                String str = EntityUtils.toString(entity);
                List<String> list = divide(str);
                String tt = getValue("name", list.get(0));
                System.out.println(tt);

            }
            catch (Exception e)
            {
            }
            finally
            {
                response.close();
            }
        }
        catch (Exception e)
        {
        }
        finally
        {
            try
            {
                client.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }