完成订单信息获取
同时下载订单快递pdf
同时获取pdf 文件内容
写一个 httpclient 请求类,完成模拟请求
public class TestUtil {
private CloseableHttpClient httpClient;
private RequestConfig config;
public TestUtil() {
config = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ConnectionKeepAliveStrategy connectionKeepAliveStrategy = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) {
return 60 * 1000;
}
};
ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setBufferSize(4128)
.build();
httpClient = HttpClients.custom().setSSLSocketFactory(sslFactory).setRetryHandler(new DefaultHttpRequestRetryHandler())
.setKeepAliveStrategy(connectionKeepAliveStrategy).setDefaultConnectionConfig(connectionConfig).build();
} catch (Exception e) {
e.printStackTrace();
httpClient = HttpClients.createDefault();
}
}
public CloseableHttpResponse formPost(String url, List<NameValuePair> para) throws Exception {
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httppost.addHeader("Cookie", "PHPSESSID=7mo5dqteclrsjc0l9o223poop1; ");
httppost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36");
httppost.addHeader("X-Requested-With", "XMLHttpRequest");
try {
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httppost.setConfig(config);
httppost.setEntity(new UrlEncodedFormEntity(para, "utf-8"));
CloseableHttpResponse res = httpClient.execute(httppost);
return res;
} catch (Exception e) {
throw e;
} finally {
}
}
完成订单信息获取
public static JSONArray getAllShopeeOrder(Integer pageNo) {
TestUtil testUtil = new TestUtil();
List<NameValuePair> para = new ArrayList<NameValuePair>();
para.add(new BasicNameValuePair("OrderPlus.isNewOrder", "2"));
para.add(new BasicNameValuePair("isshowordercombosku", "2"));
para.add(new BasicNameValuePair("page", pageNo.toString()));
para.add(new BasicNameValuePair("rowsPerPage", "500"));
para.add(new BasicNameValuePair("Order_isCloud", "2"));
para.add(new BasicNameValuePair("m", "order"));
try {
CloseableHttpResponse removeRes = testUtil.formPost("https://www.xxxxx.com/index.php?mod=order.oTc", para);
HttpEntity entity = removeRes.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
JSONArray orderDataArray = JSONObject.parseObject(result).getJSONArray("orderDataList");
return orderDataArray;
} catch (Exception e){
e.printStackTrace();
}
return null;
}
根据pdf 的URL下载pdf
public static String sendPdfToServer(String pdfUrl, String name) {
String pdfSavePath = "";
try {
URL url = new URL(pdfUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(20 * 1000);
InputStream inStream = conn.getInputStream();
byte[] data = readInputStream(inStream);
pdfSavePath = "D://pdf/" + name + ".pdf";
File imageFile = new File(pdfSavePath);
FileOutputStream outStream = new FileOutputStream(imageFile);
outStream.write(data);
outStream.close();
if (!imageFile.exists() || !imageFile.isFile()) {
System.out.println("PDF 生成失败");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(20 * 1000);
InputStream inStreamRetry = conn.getInputStream();
byte[] dataRetry = readInputStream(inStreamRetry);
File imageFileRetry = new File(pdfSavePath);
FileOutputStream outStreamimageFileRetry = new FileOutputStream(imageFileRetry);
outStreamimageFileRetry.write(dataRetry);
outStreamimageFileRetry.close();
}
return pdfSavePath;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
导入pdf 检测的包 更新maven
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.23</version>
</dependency>
利用对应的API 完成pdf的读取
public static String scanPdf(String pdfUrl) throws IOException {
String info = "";
try {
File file = new File(pdfUrl);
PDDocument document = PDDocument.load(file);
document.getClass();
PDFTextStripper tStripper = new PDFTextStripper();
tStripper.setSortByPosition(true);
info = tStripper.getText(document);
} catch (Exception e) {
e.printStackTrace();
}
return info;
}