由于自己后台记录的充值数据和苹果的结算数据有出入,所以需要做一个功能实现抓取每日苹果收入数据,按月结算,和自己内部记录的充值数据做对比算坏账,实现了掉线获取cookie重新登录功能,抓取每日收入,贴上主要代码供参考:
package ;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.protocol.HTTP;
import org.junit.Test;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.wyx.itunes.bean.ChargeInfo;
import com.wyx.itunes.util.TimeUtil;
import com.wyx.itunes.util.UtcTimeUtil;
//对接口进行测试
public class CommonRequest {
private String url = "https://reportingitc2.apple.com/api/data/timeseries";
private String charset = HTTP.UTF_8;
private HttpClientUtil httpClientUtil = null;
static Logger logger = null;
public HashMap<String, Logger> loggerMap = new HashMap<String, Logger>();
private final static org.slf4j.Logger logg = org.slf4j.LoggerFactory.getLogger(CommonRequest.class);
public static String myacinfo;
public static String itctx;
public CommonRequest(){
httpClientUtil = new HttpClientUtil();
}
static {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
logger = loggerContext.getLogger("log-itunes-info");
}
@Test
public void getLoggerPost(){
String day = UtcTimeUtil.getUTCDayofMonth();//2016-06-01T04:13:18
if(myacinfo == null || itctx == null){
authLogin();
getLoggerByType("status").info(TimeUtil.getDateTime()+" myacinfo="+myacinfo);
getLoggerByType("status").info(TimeUtil.getDateTime()+" itctx="+itctx);
}
String httpOrgCreateTest = url;//TimeUtil.getLastETC()+".000Z" TimeUtil.getDateTimeETC()+".000Z"
ChargeInfo info;
if(day.equals("1")){
info = new ChargeInfo(new Integer[]{}, null, "day", UtcTimeUtil.getMonthFirstUtc1()+".000Z",UtcTimeUtil.getMonthYesterUtc()+".000Z", "descending", 100, new String[]{"units_utc","Royalty_utc","total_tax_usd_utc"});
}else{
info = new ChargeInfo(new Integer[]{}, null, "day",UtcTimeUtil.getMonthFirstUtc()+".000Z" ,UtcTimeUtil.getMonthYesterUtc()+".000Z", "descending", 100, new String[]{"units_utc","Royalty_utc","total_tax_usd_utc"});
}
Gson gson = new Gson();
String jsonStr = gson.toJson(info);
String resData = httpClientUtil.doPost(httpOrgCreateTest,jsonStr,info,myacinfo,itctx);
System.out.println(resData);
getLoggerByType("1001001").info(TimeUtil.getDateTime()+url+" "+resData);
}
@Test
public void authLogin(){
String urlLogin = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=22d448248055bab0dc197c6271d738c3";
String itunesConn = "https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa?";
Map<String,Object> param = new HashMap<>();
param.put("accountName", "账号");
param.put("password", "密码");
param.put("rememberMe", false);
Gson gson = new Gson();
String jsonStr = gson.toJson(param);
String resData = httpClientUtil.autoLogin(urlLogin,jsonStr,charset);//myacinfo 获取登录cookie
String resConn = httpClientUtil.doCommonCookieGet(itunesConn, resData, charset);
myacinfo = resData;
itctx = resConn;
System.out.println("myacinfo="+resData);
System.out.println("itctx="+resConn);
getLoggerByType("1001006").info("myacinfo="+resData);
getLoggerByType("1001006").info("itctx="+resConn);
}
public Logger getLoggerByType(String type) {
if (type.equals("default")) {
return logger;
}
Logger ret = loggerMap.get(type);
if (ret == null) {
ret = newLogger(type);
loggerMap.put(type, ret);
}
return ret;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Logger newLogger(String type) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
RollingFileAppender rfAppender = new RollingFileAppender();
rfAppender.setContext(loggerContext);
rfAppender.setFile("logs"+File.separator+type + File.separator + getDay()+".log");
TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
rollingPolicy.setContext(loggerContext);
rollingPolicy.setParent(rfAppender);
rollingPolicy.setFileNamePattern("logs"+File.separator+type + File.separator + "%d{yyyyMMddHH}.log");
rollingPolicy.start();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%msg%n");
encoder.start();
rfAppender.setEncoder(encoder);
rfAppender.setRollingPolicy(rollingPolicy);
rfAppender.start();
Logger logbackLogger = loggerContext.getLogger(type);
logbackLogger.addAppender(rfAppender);
return logbackLogger;
}
public String getDay(){
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd");
return format.format(new Date(System.currentTimeMillis()));
}
private String url2 = "https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa/ra/nav/header/modules";
@Test
public void Test(){//抓取页面
HttpClient httpClient = new HttpClient();
StringBuilder result = new StringBuilder();
InputStream ins = null;
GetMethod method = new GetMethod("https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa?");
headerWrapper(method);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));