表題の通りなのですが、出来るでしょうか(出来るとは思うのですが)。
下記、POST用の雛形になります。
URL url = new URL(urlString); HttpURLConnection uc = (HttpURLConnection) url.openConnection(); // URLConnection uc = url.openConnection(); //POST可能にする uc.setRequestMethod("POST"); uc.setDoOutput(true); //送信許可 uc.setDoInput(true); //受信許可 //リクエストヘッダ uc.setRequestProperty("User-Agent", "@IT java-tips URLConnection"); uc.setRequestProperty("Accept-Language", "ja"); //リクエストボディ OutputStream os = uc.getOutputStream(); //POST用のOutputStreamを取得 String postStr = "data_1=bar1&data_2=bar2"; //POSTするデータ PrintStream ps = new PrintStream(os); ps.print(postStr); ps.close(); //POSTした結果を取得 InputStream is = uc.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String s; while ((s = reader.readLine()) != null) { System.out.println(s); } reader.close();
リクエストボディに直接バイナリデータをwriteする方法もあるようですが、
パラメータ名 = バイナリデータ(barの部分)
の場合を想定しています。
ご回答、お願い致します。
回答2件
あなたの回答
tips
プレビュー