It is as follows.
try {
//コネクション
URL url = new URL("URL");
httpURLConnection_ = (HttpURLConnection) url.openConnection();
httpURLConnection_.setDoOutput (true); // Enable POST
httpURLConnection_.connect();
//送信したいデータ
String param = "param1=1";
//リクエストボディに送信したいデータを書き込む
OutputStream os = httpURLConnection_.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(param);
writer.flush();
writer.close();
//クローズ処理
os.close();
} catch (IOException e) {
e.printStackTrace();
}
To get the value on the server side, do as follows. (The server is a java file that inherits HttpServlet)
public class Server extends HttpServlet{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
request.getParameter("param1");
}
}
It looked difficult, but it was easier to implement than expected. It seems to be used quite a lot, so I want to remember it.
http://konkea.hatenablog.com/entry/2016/10/20/181856
Recommended Posts