51Testing软件测试论坛

标题: HttpClient request with authentication [打印本页]

作者: starryEstrella    时间: 2018-4-30 16:24
标题: HttpClient request with authentication
        private static final CloseableHttpClient httpclient = HttpClients.createDefault();
       
          public static String sendPost(String url, Map<String, String> map) {
                List<NameValuePair> formparams = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
                HttpPost httppost = new HttpPost(url);
                httppost.setEntity(entity);
                CloseableHttpResponse response = null;
                try {
                    response = httpclient.execute(httppost);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                if (null==response)
                        return "";
                HttpEntity entity1 = response.getEntity();
                String result = null;
                try {
                    result = EntityUtils.toString(entity1);
                } catch (ParseException | IOException e) {
                    e.printStackTrace();
                }
                return result;
           }
          
          public static String sendPostWithAuth(String url, String authInfo) {
                   String username="test";
                   String password="test123";
                  
                   CloseableHttpClient httpclient = HttpClients.createDefault();
           String encoding = Base64.getEncoder().encodeToString((username+":"+ password).getBytes());
           System.out.println(encoding);

               HttpGet httpget = new HttpGet(url);
               httpget.addHeader("Authorization", "Basic " + encoding);
                CloseableHttpResponse response = null;
                try {
                    response = httpclient.execute(httpget);  
                    int status = response.getStatusLine().getStatusCode();  
                    System.out.println(String.valueOf(status));
                   
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                HttpEntity entity1 = response.getEntity();
                String result = null;
                try {
                    result = EntityUtils.toString(entity1);
                    System.out.println(result);
                } catch (ParseException | IOException e) {
                    e.printStackTrace();
                }
                return result;
           }
作者: starryEstrella    时间: 2018-4-30 16:26
Get reference from other sources,  and it is proved to be very helpful, so post this so that more persons could check this.
作者: 181lzx    时间: 2018-4-30 20:39





欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2