httpClient通过digest调用海康威视/ISAPI/Streaming/channels/1/picture接口的代码
时间: 2023-11-11 22:07:18 浏览: 128
开发调用海康威视摄像头的web端页面
5星 · 资源好评率100%
以下是使用httpClient通过digest调用海康威视/ISAPI/Streaming/channels/1/picture接口的示例代码:
```java
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.auth.DigestScheme;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HttpDigestAuthentication {
private static final String USERNAME = "admin";
private static final String PASSWORD = "12345";
private static final String URI = "http://192.168.1.100/ISAPI/Streaming/channels/1/picture";
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(URI);
// Calculate digest
DigestScheme digestScheme = new DigestScheme();
digestScheme.overrideParamter("realm", "Digest Authentication");
digestScheme.overrideParamter("nonce", "1234567890abcdef");
digestScheme.overrideParamter("qop", "auth");
digestScheme.overrideParamter("algorithm", "MD5");
digestScheme.overrideParamter("nc", "00000001");
digestScheme.overrideParamter("cnonce", "abcdef");
String digest = digestScheme.authenticate(new org.apache.http.auth.UsernamePasswordCredentials(USERNAME, PASSWORD), httpGet, null);
// Set authorization header
httpGet.setHeader("Authorization", digest);
// Execute request and read response
CloseableHttpResponse response = httpClient.execute(httpGet);
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody);
// Close resources
response.close();
httpClient.close();
}
}
```
请注意,这只是一个示例代码,实际应用中,需要根据海康威视设备的实际情况调整参数。
阅读全文