java发送post下载文件,在发送数据时使用POST下载R中的文件

我尝试下载一个文件,从我需要同时发送数据的服务器上获取它 . 在命令行使用curl它工作正常:

curl "https://www.ishares.com/us/product-screener-download.dl" --data "productView=ishares&portfolios=239561-239855"

不幸的是我没有得到它与R一起工作 . 我尝试使用lib.l,download.file与libcurl,curl_download和httr . (使用curl或wget的download.file不起作用,因为我在窗口机器上 . )

我尝试过但没有使用curl:

library("curl")

handle

handle_setopt(handle, customrequest = "POST")

handle_setform(handle, productView="ishares",portfolios="239561-239855")

curl_download("https://www.ishares.com/us/products/etf-product-list", "./data/ishares-us-etf.xls", handle=handle)

我尝试过什么并没有使用httr:

library(httr)

POST("https://www.ishares.com/us/products/etf-product-list", body = list(productView="ishares",portfolios="239561-239855"))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java发送POST请求使用multipart/form-data格式进行数据传输可以使用Java的HttpURLConnection类来实现。下面是一个简单的示例代码: ```java import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class MultipartFormDataExample { public static void main(String[] args) throws IOException { // 请求URL String requestUrl = "http://example.com/upload"; // 构建请求参数 String boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"; String lineBreak = "\r\n"; String postData = "--" + boundary + lineBreak + "Content-Disposition: form-data; name=\"file\"; filename=\"example.txt\"" + lineBreak + "Content-Type: text/plain" + lineBreak + lineBreak + "This is the content of the file." + lineBreak + "--" + boundary + "--"; // 创建URL对象 URL url = new URL(requestUrl); // 创建HttpURLConnection对象,并设置请求方法为POST HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); // 设置请求头 connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); // 允许输入输出流 connection.setDoInput(true); connection.setDoOutput(true); // 获取输出流 OutputStream outputStream = connection.getOutputStream(); // 写入请求参数 outputStream.write(postData.getBytes()); // 关闭输出流 outputStream.close(); // 获取响应码 int responseCode = connection.getResponseCode(); // 读取响应内容 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); // 输出响应结果 System.out.println("Response Code: " + responseCode); System.out.println("Response Content: " + response.toString()); } } ``` 上述代码,我们首先构建了请求参数postData,其包含了一个文件字段file,以及文件的内容。然后创建URL对象和HttpURLConnection对象,并设置请求方法为POST。接着设置请求的Content-Type为multipart/form-data,并允许输入输出流。然后获取输出流,将请求参数写入输出流,并关闭输出流。最后获取响应码和响应内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值