package com.hym.http;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.zip.GZIPInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class GetDataFromSportEngine {
private static String SUB_REQ = "http://xxx.xxxx.com:8081/xmlfeed?requestType=SubscribeRequest&subscriptionSpecificationName=football";
private static String GET_NEXT_INIT_DATA_REQ = "http://xxx.xxxx.com:8081/xmlfeed?requestType=GetNextInitialDataRequest&subscriptionId=";
private static String GET_NEXT_UPDATE_DATA_RQE = "http://xxx.xxxx.com:8081/xmlfeed?requestType=GetNextUpdateDataRequest&subscriptionId=";
private static String UNSUB_REQ = "http://xxx.xxxx.com:8081/xmlfeed?requestType=UnsubscribeRequest&subscriptionSpecificationName=football&subscriptionId=";
private static String localPath = "D:\\localPath\\";
public static void main(String args[]) throws Exception {
getData();
}
public static void getData() throws Exception {
DefaultHttpClient httpclient = getLongConnect();
System.out.println("********** Begin crash data from BetBrain");
String subscriptionId = getSubscriptionId(httpclient);
if (subscriptionId == null) {
return;
}
// 5.Steps 3 and 4 repeat until the initial data dump is over.
getInitSub(httpclient, subscriptionId);
// 6.Client sends to Sports Engine a get-next-update-data-request SDQL
// construct.
// 7.Sports Engine sends to Client a get-next-update-data-response SDQL
// construct.
getNextUpdate(httpclient, subscriptionId);
// 8.Steps 6 and 7 repeat until Client sends an unsubscribe-request SDQL
// construct.
sendUnSub(httpclient, subscriptionId);
}
public static String getSubscriptionId(DefaultHttpClient httpclient)
throws Exception {
// 1.Client sends to Sports Engine a subscribe-request SDQL construct.
HttpGet httpGet = new HttpGet(SUB_REQ);
HttpResponse response1 = httpclient.execute(httpGet);
// 2.Sports Engine sends to Client a subscribe-response SDQL construct.
HttpEntity entity1 = response1.getEntity();
InputStream in = entity1.getContent();
String xmlStr = unGZip(in);
System.out.println("getSubscriptionId the xmlStr = " + xmlStr);
List<String> lines = new ArrayList<String>();
lines.add(xmlStr);
FileUtils
.writeLines(new File(localPath + "/SubscriptionId.xml"), lines);
String subscriptionId = parseXMLAndGetSubscriptionId(xmlStr.toString());
System.out.println("1. subscriptionId = " + subscriptionId);
return subscriptionId;
}
private static String parseXMLAndGetSubscriptionId(String xmlStr) {
if (xmlStr == null) {
return null;
}
String subscriptionId = null;
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(new ByteArrayInputStream(xmlStr
.getBytes("UTF-8")));
Element root = document.getRootElement();
Iterator<Element> iter = root.elementIterator("SubscribeResponse");
while (iter.hasNext()) {
Element ele = iter.next();
Attribute attr = ele.attribute("subscriptionId");
subscriptionId = attr.getStringValue();
}
} catch (Exception e) {
e.printStackTrace();
}
return subscriptionId;
}
public static void getInitSub(DefaultHttpClient httpclient,
String subscriptionId) throws Exception {
System.out.println("2. getInitSub ");
boolean isComplete = false;
while (!isComplete) {
// 3.Client sends to Sports Engine a get-next-initial-data-request
// SDQL construct.
HttpGet httpGet2 = new HttpGet(GET_NEXT_INIT_DATA_REQ
+ subscriptionId);
// 4.Sports Engine sends to Client a get-next-initial-data-response
// SDQL construct.
HttpResponse response2 = httpclient.execute(httpGet2);
HttpEntity entity2 = response2.getEntity();
InputStream in2 = entity2.getContent();
String xmlStr = unGZip(in2);
List<String> lines = new ArrayList<String>();
lines.add(xmlStr);
FileUtils
.writeLines(new File(localPath + "/InitSub_"+System.currentTimeMillis()+ ".xml"), lines);
System.out.println("getInitSub the xmlStr = " + xmlStr);
String dumpComplete = parseXMLAndGetDumpComplete(xmlStr);
System.out.print("dumpComplete = " + dumpComplete);
if ("true".equals(dumpComplete)) {
isComplete = true;
}
Thread.sleep(35000);
}
}
/**
* gZip解压方法, 直接返回gz中文本的内容
* */
private static String unGZip(InputStream inputStream) {
byte[] b = null;
String s = null;
try {
GZIPInputStream gzip = new GZIPInputStream(inputStream);
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
s = baos.toString();
baos.flush();
baos.close();
gzip.close();
// bis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return s;
}
private static String parseXMLAndGetDumpComplete(String xmlStr) {
if (xmlStr == null) {
return null;
}
String dumpComplete = null;
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(new ByteArrayInputStream(
xmlStr.getBytes("UTF-8")));
Element root = document.getRootElement();
Iterator<Element> iter = root.elementIterator("GetNextInitialDataResponse");
while (iter.hasNext()) {
Element recordEle = (Element) iter.next();
Iterator<Element> it = recordEle.elementIterator("InitialData");
while (it.hasNext()) {
Element ele = (Element) it.next();
Attribute attr = ele.attribute("dumpComplete");
dumpComplete = attr.getStringValue();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return dumpComplete;
}
public static void getNextUpdate(DefaultHttpClient httpclient,
String subscriptionId) throws Exception {
System.out.println("3. Begin getNextUpdate");
int count = 0;
boolean exit = false;
while (!exit) {
HttpGet httpGet2 = new HttpGet(GET_NEXT_UPDATE_DATA_RQE
+ subscriptionId);
HttpResponse response2 = httpclient.execute(httpGet2);
HttpEntity entity2 = response2.getEntity();
InputStream in2 = entity2.getContent();
String fileName = "nextUpdate_" + System.currentTimeMillis()
+ ".gz";
// String xmlStr = unGZip(in2);
// List<String> lines = new ArrayList<String>();
// lines.add(xmlStr);
// FileUtils.writeLines(new File(localPath + fileName), lines);
FileUtils.copyInputStreamToFile(in2, new File(localPath + fileName));
Thread.sleep(60000);
count++;
System.out.println("getNextUpdate count = " + count);
if (count == 720) {
exit = true;
}
}
}
public static void sendUnSub(DefaultHttpClient httpclient,
String subscriptionId) throws Exception {
HttpGet httpGet = new HttpGet(UNSUB_REQ + subscriptionId);
HttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
// List<String> lines = IOUtils.readLines(in, "UTF-8");
// Iterator<String> iter = lines.iterator();
// StringBuffer xmlStr = null;
// while (iter.hasNext()) {
// xmlStr.append(iter.next());
// }
String xmlStr = unGZip(in);
System.out.println("****** The end, get the UnSub xml: "
+ xmlStr.toString());
}
private static String parseXMLAndGetResponseCode(String xmlStr) {
String code = null;
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(new ByteArrayInputStream(
xmlStr.getBytes("UTF-8")));
Element root = document.getRootElement();
Iterator<Element> iter = root.elementIterator("UnsubscribeResponse");
while (iter.hasNext()) {
Element ele = iter.next();
Attribute attr = ele.attribute("code");
code = attr.getStringValue();
}
} catch (Exception e) {
e.printStackTrace();
}
return code;
}
public static DefaultHttpClient getLongConnect() {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response,
HttpContext context) {
// Honor 'keep-alive' header
HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch (NumberFormatException ignore) {
}
}
}
HttpHost target = (HttpHost) context
.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if ("seps.betbrain.com".equalsIgnoreCase(target.getHostName())) {
// Keep alive for 5 seconds only
return 5 * 1000;
} else {
// otherwise keep alive for 30 seconds
return 30 * 1000;
}
}
});
return httpclient;
}
}
分享到:
相关推荐
在Java中解析这种类型的文件,通常需要两个步骤:首先解压gzip,然后解包tar。这里我们将详细探讨两种不同的方法来实现这个过程。 方法一:使用Apache Commons Compress库 Apache Commons Compress库是Java社区...
在C语言中解析XML文件,我们可以利用开源库libxml2,这是一个功能强大且广泛使用的库,提供了XML处理的各种功能,包括解析、验证、序列化和XPath查询。 在Linux环境下,要使用libxml2库,首先需要下载并安装。从ftp...
这些API可能包括创建解析器对象、解析XML文档、获取元素和属性、处理命名空间等功能。在编程语言如C、Java或Python中,开发者可以调用这些接口来实现对XML数据的操作。 XML解析器有多种类型,如DOM(Document ...
"et_xmlfile-1.0.1.tar.gz"是一个压缩包文件,它的名称暗示着它包含了一个名为"et_xmlfile"的软件库的版本1.0.1。这个压缩包采用了tar和gzip两种常见的文件打包和压缩格式。在Unix/Linux环境中,tar常用于将多个文件...
1. **解析XML文档**:XML-Simple能够将XML字符串或文件解析成Perl的数据结构。例如,一个XML文档可以被解析成一个哈希,其中XML元素成为哈希键,元素内容成为对应的值。 2. **生成XML文档**:同样,Perl的数据结构...
1. 解压下载的“XML-LibXML-2.0126.tar.gz”文件。 2. 使用编译工具(如make)配置、编译和安装源代码。 3. 在项目中链接到LibXML库,导入必要的头文件。 4. 编写代码,调用LibXML提供的函数来解析、操作或生成XML...
随着XML(可扩展标记语言)在数据交换与存储领域的广泛应用,掌握如何有效地读取和解析XML文件成为了开发人员的一项必备技能。在C++环境中,libxml2库因其功能强大且易于使用而成为处理XML数据的首选工具之一。本文...
1. **解压IPK文件**:首先,我们需要使用归档工具(如7-Zip或tar)解压缩IPK文件,这会得到一个包含控制信息(control.tar.gz)和文件内容(data.tar.gz)的结构。 2. **解析控制信息**:控制信息包含IPK的元数据,...
本文将详细解析“choose_xml.tar.gz”这个压缩包,它包含了2021年华为光猫针对全国各地区的配置文件,旨在帮助用户更好地理解和管理他们的光猫设备。 “choose_xml”这个标题暗示了该压缩包的核心内容,即一系列XML...
MiniXML的核心功能是解析XML文档并生成新的XML文档。它提供了简单易用的API接口,使得程序员可以方便地读取XML文件的内容,提取所需的数据,并创建或修改XML结构。由于MiniXML是用C语言编写的,因此它具备了C语言的...
- 解压 "expat-2.3.0.tar.gz" 文件后,通常会有一个包含源代码、配置脚本、Makefile 等的目录结构。 - 开发者需要通过 `./configure` 脚本来检测系统环境并生成适应的 Makefile,然后使用 `make` 编译代码,最后...
1. **源代码**:`.c`和`.h`文件,这些文件包含了库的核心功能,如解析XML文档、创建和操作XML元素、处理属性等。 2. **头文件**:`.h`文件定义了库的API接口,供用户在自己的C程序中调用。 3. **示例**:可能包含...
1. 解析XML文件:使用`TiXMLDocument::LoadFile()`或`TiXMLDocument::Parse()`方法加载XML内容。 2. 遍历解析树:通过`FirstChildElement()`, `NextSiblingElement()`等方法遍历XML元素。 3. 访问和修改元素属性:...
使用这些库,开发者可以通过解析XML文档来获取其中的数据,例如节点值、属性等。 例如,使用pugixml库读取XML文件的简单步骤如下: 1. 包含头文件并创建解析器: ```cpp #include <pugixml.hpp> pugi::xml_...
例如,它可能包含了用于解析XML文档的函数,如`parse()`,以及创建新XML结构的方法,如`Element()`。此外,库可能还提供了处理命名空间、XPath查询、XML Schema验证等功能,以满足不同场景的需求。 使用XML库时,...
`validate-bes-xml`库的主要功能是解析和检查与Blackberry Enterprise Server相关的XML配置文件。Blackberry Enterprise Server是一种企业级解决方案,允许组织管理和控制Blackberry设备的安全性和功能。XML文件通常...
《PyPI官网下载 | xml-cleaner-1.0.1.tar.gz:Python库解析与应用》 在Python的生态系统中,PyPI(Python Package Index)是最重要的资源库,它为开发者提供了一个广泛的平台来分享和下载各种Python库。本文将详细...
在实际应用中,解压pugixml-1.9.tar.gz后,开发人员会得到一个包含源代码的目录结构,通常包括头文件、源代码文件和构建脚本。接下来,他们需要根据项目需求配置、编译并链接这个库,以便在自己的应用程序中使用pugi...
2. **解析测试数据**:解析XML报告后,可以进一步分析测试数据,例如统计总的测试用例数量、成功和失败的用例数、错误和失败的区别等。 3. **创建JUnit XML报告**:如果Python测试框架不直接支持生成JUnit XML,...
开发者或系统管理员通常会解压这个文件以获取源代码,然后编译和安装到他们的系统中。 2. "note.txt" - 这可能是包含构建、安装或使用libxml2时的一些注意事项或指南的文本文件,例如依赖项、配置步骤或潜在问题的...