java 代码实现下载微信支付交易账单
时间: 2023-11-12 11:05:22 浏览: 173
以下是 Java 代码实现下载微信支付交易账单的示例:
```java
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadWeChatBill {
public static void main(String[] args) {
String urlStr = "https://api.mch.weixin.qq.com/pay/downloadbill";
String data = "appid=APPID&mch_id=MCH_ID&nonce_str=NONCE_STR&sign=SIGN&bill_date=BILL_DATE&bill_type=BILL_TYPE";
String filePath = "D:/bill.csv";
try {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(data.getBytes().length));
OutputStream outputStream = conn.getOutputStream();
outputStream.write(data.getBytes());
outputStream.flush();
outputStream.close();
InputStream inputStream = conn.getInputStream();
byte[] buffer = new byte[1024];
int len;
FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath));
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.close();
inputStream.close();
System.out.println("微信支付交易账单下载成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文