技术交流QQ群 933925017
java httpClient Digest Auth 认证
因为项目需要,请求海康摄像头,进行抓图以及云台控制等功能, 海康有http协议,但是需要进行请求头认证,因为海康给的资料已经过时,所以找了很久

1.先上postMan测试通过

2.httpClient代码 - 发送普通的请求

DefaultHttpClient httpclient = new DefaultHttpClient();
String hostUrl = "http://172.16.1.11";
// 抓图
//        String url = hostUrl + "/ISAPI/Streaming/channels/1/picture";
// 获取焦距
String url = hostUrl + "/ISAPI/PTZCtrl/channels/1/absoluteEx/capabilities";
// 172.16.1.11/ISAPI/PTZCtrl/channels/1/capabilities
URI serverURI = new URI(url);
HttpGet httpGet = new HttpGet(url);
String username = "admin";
String password = "abcd1234";
Credentials creds = new UsernamePasswordCredentials(username,password);
httpclient.getCredentialsProvider().setCredentials(new AuthScope(serverURI.getHost(), serverURI.getPort()), (Credentials) creds);HttpResponse response = httpclient.execute(httpGet);String resultString = EntityUtils.toString(response.getEntity(), "utf-8");JSONObject object = Xml2Json.xml2Json(resultString);
System.out.println(object);

3.httpClient 下载图片

// 抓图public static void download(String url, String filePathName) throws URISyntaxException {DefaultHttpClient httpclient = new DefaultHttpClient();URI serverURI = new URI(url);String username = "admin";String password = "abcd1234";Credentials creds = new UsernamePasswordCredentials(username,password);httpclient.getCredentialsProvider().setCredentials(new AuthScope(serverURI.getHost(), serverURI.getPort()), (Credentials) creds);try {HttpGet httpget = new HttpGet(url);//伪装成google的爬虫JAVA问题查询httpget.setHeader("User-Agent", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");// Execute HTTP requestSystem.out.println("executing request " + httpget.getURI());HttpResponse response = httpclient.execute(httpget);File storeFile = new File(filePathName);FileOutputStream output = new FileOutputStream(storeFile);// 得到网络资源的字节数组,并写入文件HttpEntity entity = response.getEntity();if (entity != null) {InputStream instream = entity.getContent();try {byte b[] = new byte[1024];int j = 0;while ((j = instream.read(b)) != -1) {output.write(b, 0, j);}output.flush();output.close();} catch (IOException ex) {throw ex;} catch (RuntimeException ex) {httpget.abort();throw ex;} finally {try {instream.close();} catch (Exception ignore) {}}}} catch (Exception e) {
//            logger.error(e.getMessage(), e);} finally {httpclient.getConnectionManager().shutdown();}}

4.核心代码,设置请求认证

 DefaultHttpClient httpclient = new DefaultHttpClient();URI serverURI = new URI(url);String username = "admin";String password = "abcd1234";Credentials creds = new UsernamePasswordCredentials(username,password);httpclient.getCredentialsProvider().setCredentials(new AuthScope(serverURI.getHost(), serverURI.getPort()), (Credentials) creds);

java httpClient Digest Auth 认证相关推荐

  1. Digest Auth 认证

    本人写的Digest 认证一次通过,不需要按照常规的方式来 public void postMethod(String url, String query) {try {CredentialsProv ...

  2. http请求digest auth认证

    1.post请求 public static String postMethod(String url, String query,String host) throws IOException {S ...

  3. curl第六课 Http Digest Auth认证应用

    项目          在安迅士摄像机网页上,配置系统选项,HTTP/RTSP Password Settings 中, 选择Encrypted only.获取设备的云台状态信息,使用的是摘要认证 例 ...

  4. 4种Spring Boot 实现通用 Auth 认证方式

    摘要: 文章介绍了 spring-boot 中实现通用 auth 的四种方式,包括传统 AOP.拦截器.参数解析器和过滤器,并提供了对应的实例代码,最后简单总结了下它们的执行顺序. 本文分享自华为云社 ...

  5. Rest Api 项目添加 Basic Auth 认证

    title: Rest Api 项目添加 Basic Auth 认证 date: 2022-10-10 22:09 tags: [Java,Spring Boot,Rest Api,Basic Aut ...

  6. 关于HttpClient绕过SSL认证以及NTLM认证

    前言 本篇文章只涉及本人在工作上使用HttpClient遇到的情况,并不会详细地展开讲如何使用HttpClient. 1. 为什么使用HttpClient? 一开始其实是考虑使用RestTemplat ...

  7. Laravel 5.5 使用 Passport 实现 Auth 认证

    最近在写一个前后端分离项目,本来想用 Jwt-auth + Dingo 开发的,但是略感笨重,于是想到了 Laravel 的 Passport 和 5.5 新出的 Api Resource.Larav ...

  8. django CBV装饰器 自定义django中间件 csrf跨站请求伪造 auth认证模块

    CBV加装饰器 第一种 @method_decorator(装饰器) 加在get上 第二种 @method_decorator(login_auth,name='get') 加在类上 第三种 @met ...

  9. framework —— auth认证

    ramework -- auth认证 1.目录结构 2.urls.py from django.conf.urls import url from django.contrib import admi ...

最新文章

  1. 杨兴平离职完全是意料之中
  2. mysql 保留两位小数
  3. linux查看端口所占用的进程号
  4. python 装饰器有哪些_python装饰器有什么用
  5. [DIV/CSS] 【译】60个有用CSS代码片段
  6. MySQL笔记-MDL锁(metadata lock)
  7. 首发骁龙898!小米12系列屏幕方案曝光:双曲面屏+全新封装工艺
  8. 基于MNIST数据集的不同权重初始值的比较
  9. Problem E: 深入浅出学算法019-求n的阶乘
  10. New Adventure----GUI Design Studio
  11. 网易云课堂Java模拟面试笔记(31-40)
  12. oracle 白鳝 row cache lock,ROW CACHE LOCK导致数据库CPU高案例分析
  13. 【你好,windows】Windows 10 X64 21H1 19043.964 纯净版2021.5.2
  14. 什么是SOCKS5代理 它的原理是什么
  15. Unity Shader - 伪次表面散射模拟
  16. 迈卡名车茂品牌LOGO全新升级
  17. 深度学习训练时GPU利用率忽高忽低问题
  18. 朱有鹏-3.linux应用编程和网络编程-第3部分-3.3.获取系统信息
  19. 【从0到1搭建LoRa物联网】8、国产LoRa终端ASR6505 PingPong通信
  20. 174-C语言入门(进制,补码,类型)

热门文章

  1. WordPress主题-大前端DUX主题初墨博客修改优化增强版ZUX介绍
  2. 深入浅出Git教程+一个小时学会Git(转载)(堪称完美)
  3. 电机及电气技术实验装置QY-503
  4. 初识思科模拟器7.2
  5. 彻底解决快压这个垃圾流氓软件
  6. Excel取消掉每次打开弹出的自定义项安装程序弹窗
  7. YOLOV7调试的bug
  8. think-cell 无法安装怎么解决丨安装教程丨解决方法
  9. Pikachu CSRF GET/POST/TOKE级别漏洞实战Pikachu exec “ping“ exec “evel“OS命令执行漏洞
  10. STM32的8种IO口的模式