通过一个例子,我使用以下代码下载文件:
url = new URL(stUrl);
connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty ("Foo", "Bar");
connection.setDoOutput(true); // update as per comment!
connection.connect();
// download the file
is = new BufferedInputStream(
url.openStream(),
8192
);
os = new BufferedOutputStream( new FileOutputStream( tmp ) );
copyStream( is, os );
这适用于下载。但是在http请求中包括Foo/Bar
报头失败。
为什么?
更新:
- 下载url="http://x0data.com/android/default/rqst"
- 我想传递标头(而不是GET或POST参数)
- 我知道服务器还可以,因为这曾经是有效的(对于我似乎已经丢失的Android代码…)
好的,有@greenapps的一些很棒的帮助。下载文件时,以下内容似乎包含标题:
url = new URL(stUrl);
connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty ("Foo", "Bar");
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
现在,我只需要弄清楚如何将BufferedReader
写入File
。。。
(并测试和验证…)