SSブログ

Javaのソース上からPOSTしたい。 [Java・JSP]

できました。

PrintStream ps = null;
HttpURLConnection urlConn = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 指定URLへの接続
URL theUrl = new URL(url);
urlConn = (HttpURLConnection)theUrl.openConnection();
// POST
urlConn.setDoOutput(true);
urlConn.setRequestProperty("User-Agent", "@TWR e-wellness HYBRID URLConnection");
urlConn.setRequestProperty("Accept-Language", "ja");
OutputStream os = urlConn.getOutputStream();
ps = new PrintStream(os);
ps.print("arg1=111&arg2=222&arg3=333");
// POST結果の入力
bis = new BufferedInputStream(urlConn.getInputStream());
byte[] buffer = new byte[8192];
int size = bis.read(buffer);
if (size < 0) { return false; }
// クライアントへの出力
bos = new BufferedOutputStream(response.getOutputStream());
while (0 <= size) {
bos.write(buffer, 0, size);
size = bis.read(buffer);
}
bos.flush();
}
catch (Exception ex) {
// 例外処理
}
finally {
if (null != ps) { ps.close(); }
if (null != bos) { bos.close(); }
if (null != bis) { bis.close(); }
if (null != urlConn) { urlConn.disconnect(); }
}


nice!(0)  コメント(0)  トラックバック(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。