怎么用java程序以post方式发送表单参数给服务器
展开全部
POST方式发送请求示例:
String fullurl = url;
//打开连接
URLConnection conn =new URL(fullurl).openConnection();
//设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36");
//发送POST请求必须设置下面两行
conn.setDoInput(true);
conn.setDoOutput(true);
try (
//获取URLConnection对象对应的输出流
PrintWriter out = new PrintWriter(conn.getOutputStream());) {
out.print(parm);//发送请求参数(key1=value1&key2=value2)
out.flush();//flush输出流的缓冲
} catch (Exception e) {}
//获取响应头字段
Map<String, List<String>> map = conn.getHeaderFields();
//根据输入流读取响应数据
InputStream is = conn.getInputStream();
仅供参考。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询