| | |
| | | import org.apache.http.auth.UsernamePasswordCredentials; |
| | | import org.apache.http.client.CredentialsProvider; |
| | | import org.apache.http.client.config.RequestConfig; |
| | | import org.apache.http.client.methods.CloseableHttpResponse; |
| | | import org.apache.http.client.methods.HttpGet; |
| | | import org.apache.http.impl.client.BasicCredentialsProvider; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClientBuilder; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.util.EntityUtils; |
| | | import org.w3c.dom.Document; |
| | | import org.w3c.dom.Element; |
| | | import org.w3c.dom.Node; |
| | | import org.w3c.dom.NodeList; |
| | | import org.w3c.dom.*; |
| | | import org.xml.sax.InputSource; |
| | | import org.xml.sax.SAXException; |
| | | |
| | |
| | | public class HKApi { |
| | | public static OSDResult getOsdByIP(String ip, String userName, String password) { |
| | | OSDResult osdResult = new OSDResult(); |
| | | CloseableHttpClient httpClient = HttpClients.createDefault(); |
| | | String hostUrl = "http://" + ip; |
| | | try { |
| | | //获取OSD |
| | | HttpResponse OSDResponse = getHttpResponse(httpClient, hostUrl + ApiConstants.HK_OSD_PATH, userName, password); |
| | | String OSDString = EntityUtils.toString(OSDResponse.getEntity(), "utf-8"); |
| | | String OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH, userName, password); |
| | | if (OSDString == null) return null; |
| | | |
| | | //解析xml |
| | | parseXMl(OSDString, "TextOverlay", "displayText", osdResult, null); |
| | | parseXMl(OSDString, "TextOverlay", "displayText", osdResult, null,ip); |
| | | //获取Time |
| | | HttpResponse TimeResponse = getHttpResponse(httpClient, hostUrl + ApiConstants.HK_OSD_TIME, userName, password); |
| | | String timeString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_TIME, userName, password); |
| | | if (timeString == null) return null; |
| | | |
| | | Date date = new Date(); |
| | | String timeString = EntityUtils.toString(TimeResponse.getEntity(), "utf-8"); |
| | | //解析xml |
| | | parseXMl(timeString, "Time", "localTime", osdResult, date); |
| | | parseXMl(timeString, "Time", "localTime", osdResult, date,ip); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | return osdResult; |
| | | } |
| | | |
| | | private static void parseXMl(String OSDString, String tagName1, String tagName2, OSDResult osdResult, Date date) throws ParserConfigurationException, SAXException, IOException { |
| | | private static void parseXMl(String OSDString, String tagName1, String tagName2, OSDResult osdResult, Date date,String ip) throws ParserConfigurationException, SAXException, IOException { |
| | | |
| | | try { |
| | | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| | | DocumentBuilder builder = factory.newDocumentBuilder(); |
| | | Document document = builder.parse(new InputSource(new StringReader(OSDString))); |
| | |
| | | osdResult.setOsdTime(Date.from(dateTime.toInstant())); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("ip:{}出现异常,{}",ip,e.getMessage()); |
| | | throw new RuntimeException(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | private static HttpResponse getHttpResponse(CloseableHttpClient httpClient, String url, String userName, String password) throws URISyntaxException, IOException { |
| | | private static String getHttpResponse(String url, String userName, String password) throws URISyntaxException, IOException { |
| | | CredentialsProvider credsProvider = new BasicCredentialsProvider(); |
| | | credsProvider.setCredentials( |
| | | new AuthScope(new URI(url).getHost(), new URI(url).getPort()), |
| | | new UsernamePasswordCredentials(userName, password)); |
| | | // 绑定凭证提供者到HttpClient |
| | | CloseableHttpClient httpClient = HttpClientBuilder.create() |
| | | .setDefaultCredentialsProvider(credsProvider) |
| | | .build(); |
| | | |
| | | HttpGet httpGet = new HttpGet(url); |
| | | |
| | |
| | | .setSocketTimeout(1000) |
| | | .build(); |
| | | httpGet.setConfig(requestConfig); |
| | | |
| | | // 绑定凭证提供者到HttpClient |
| | | httpClient = HttpClientBuilder.create() |
| | | .setDefaultCredentialsProvider(credsProvider) |
| | | .build(); |
| | | try { |
| | | return httpClient.execute(httpGet); |
| | | } catch (Exception e){ |
| | | throw new RuntimeException("海康OSD执行失败"); |
| | | try (CloseableHttpResponse response = httpClient.execute(httpGet)) { |
| | | int statusCode = response.getStatusLine().getStatusCode(); |
| | | if (statusCode == 200) { |
| | | return EntityUtils.toString(response.getEntity(), "utf-8"); |
| | | } else { |
| | | return null; |
| | | } |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("海康OSD执行失败", e); |
| | | } |
| | | |
| | | } |
| | | } |