public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/limit_demo/limit");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setDoInput(true) ;//设置使用输入流默认为true
//设置post请求传递参数
connection.setDoOutput(true) ;//设置使用输出流,默认为false
OutputStream out = connection.getOutputStream() ;
OutputStreamWriter writer = new OutputStreamWriter(out) ;
writer.write("name=tu&gender=false") ;
writer.close() ;
connection.connect() ;//此方法在这里没有作用,调用getInputStream()方法时才开始链接服务器
//链接服务器
connection.getInputStream().close() ;
connection.disconnect() ;//断开链接,貌似也没有作用,建议写上
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
URL url = new URL("http://localhost:8080/limit_demo/limit");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setDoInput(true) ;//设置使用输入流默认为true
//设置post请求传递参数
connection.setDoOutput(true) ;//设置使用输出流,默认为false
OutputStream out = connection.getOutputStream() ;
OutputStreamWriter writer = new OutputStreamWriter(out) ;
writer.write("name=tu&gender=false") ;
writer.close() ;
connection.connect() ;//此方法在这里没有作用,调用getInputStream()方法时才开始链接服务器
//链接服务器
connection.getInputStream().close() ;
connection.disconnect() ;//断开链接,貌似也没有作用,建议写上
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}