Как отправить POST-запрос в Java?
String data = URLEncoder.encode("mydata", "UTF-8") + "=" + URLEncoder.encode("mydatavalue", "UTF-8");
data += "&" + URLEncoder.encode("mydata2", "UTF-8") + "=" + URLEncoder.encode("mydatavalue2", "UTF-8");
try {
// Send the request
URL url = new URL("http://example.com");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
//write parameters
writer.write(data);
writer.flush();
// Get the response
StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
answer.append(line);
}
writer.close();
reader.close();
}
catch (Exception e) {
//handle exception
}
Комментариев нет:
Отправить комментарий