概述
private static String Post(String url) throws IOException{ CloseableHttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(url); ProtocolVersion protocolVersion = post.getProtocolVersion(); System.out.println(protocolVersion.getProtocol()); try { //该网页需要认证(用户名、密码) HttpClientContext context = new HttpClientContext(); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(admin, password)); context.setCredentialsProvider(credentialsProvider); CloseableHttpResponse execute = client.execute(post, context); //----以下一样 HttpEntity entity = execute.getEntity(); InputStream in = entity.getContent(); StringBuilder builder = new StringBuilder(); BufferedReader bufreader = new BufferedReader(new InputStreamReader(in)); for (String temp = bufreader.readLine(); temp != null; temp = bufreader.readLine()) { builder.append(temp); } return builder.toString(); } catch (ClientProtocolException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
最后
以上就是正直烤鸡为你收集整理的http请求认证账号密码的全部内容,希望文章能够帮你解决http请求认证账号密码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复