美文网首页
Java - httpclient

Java - httpclient

作者: 尼尔君 | 来源:发表于2018-07-26 10:49 被阅读0次

    get方式

    
        CloseableHttpClient client = HttpClients.createDefault();
            
            URIBuilder uriBuilder = new URIBuilder("http://www.baidu.com/s");
            
            uriBuilder.addParameter("wd", "马士兵");
            
            
        
            
            HttpGet get = new HttpGet(uriBuilder.build());
            
            
            CloseableHttpResponse response = client.execute(get);
            
            HttpEntity httpEntity = response.getEntity();
            
            String string = EntityUtils.toString(httpEntity, "utf-8");
            
            System.out.println(string);
            
        }
    

    post方式

    
    CloseableHttpClient client = HttpClients.createDefault();
            
            
            HttpPost httpPost = new HttpPost("http://localhost/login");
            
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            
            
            list.add(new BasicNameValuePair("username", "admin"));
            
            list.add(new BasicNameValuePair("password", "123451"));
            
            StringEntity strEntity = new UrlEncodedFormEntity(list,"utf-8");
            
            httpPost.setEntity(strEntity);
            
            CloseableHttpResponse response = client.execute(httpPost);
            
            String str = EntityUtils.toString(response.getEntity(),"utf-8");
            
            System.out.println(str);
            
            response.close();
            
            client.close();
            
            
            
            
    

    相关文章

      网友评论

          本文标题:Java - httpclient

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