6

POSTリクエスト(htmlフォームなど)を送信してファイル(HTTPヘッダー: "Content-Disposition:attachment; filename =" myfile.pdf ")を取得したいのですが。

4

2 に答える 2

11

最善のオプションは、おそらくHttpClientHTMLUnitなどのサードパーティライブラリを使用することです。

標準のAPIでそれを行うことを好む場合、それはそれほど複雑ではありません。

// Construct data
String data = URLEncoder.encode("key1", "UTF-8") + "=" + 
                                URLEncoder.encode("value1", "UTF-8");

data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" +
                                URLEncoder.encode("value2", "UTF-8");

// Send data
URL url = new URL("http://hostname:80/cgi");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

// Get the response
BufferedReader rd = new BufferedReader(
        new InputStreamReader(conn.getInputStream()));

String line;
while ((line = rd.readLine()) != null) {
    // Process line...
}
wr.close();
rd.close();
于 2011-03-01T19:48:07.133 に答える
6

HttpClientを確認してください。ここにかなり包括的なチュートリアルがあります。

于 2011-03-01T19:46:40.530 に答える