美文网首页
httpClient

httpClient

作者: Vijay_ | 来源:发表于2018-03-14 13:06 被阅读8次

发送请求

 RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(5000)
                .setConnectionRequestTimeout(5000)
                .setSocketTimeout(5000)
                .build();
        HttpClient client = HttpClientBuilder.create().build();
//        get请求
//        HttpResponse response = client.execute(new HttpGet("http://blog.csdn.net/penyoudi1/article/details/73609049"));
        
//        参数
        List<BasicNameValuePair> params = Arrays.asList(
                new BasicNameValuePair("page", "1"),
                new BasicNameValuePair("rows", "20"));
//        post请求
        HttpUriRequest requestBuilder = RequestBuilder
                .post()
                .setUri("http://api.shanjiezhifu.com/kadi/goods/list")
                .addParameters(params.toArray(new NameValuePair[params.size()]))
                .setConfig(config).build();
        HttpResponse response = client.execute(requestBuilder);

        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            String string = EntityUtils.toString(entity, "utf8");
            System.out.println(string);
        } else {
            System.out.println(response.getStatusLine().getReasonPhrase());
            throw new Exception("请求失败");
        }

相关文章

网友评论

      本文标题:httpClient

      本文链接:https://www.haomeiwen.com/subject/lplfqftx.html