JAVA模拟各种请求方式访问RESTFUL

来源:互联网 发布:恋爱天数计算器软件 编辑:程序博客网 时间:2024/06/05 23:08
//需要请求的restful地址URL url = new URL("http://localhost:8080/JerseyTest/rest/hello");        //打开restful链接HttpURLConnection conn = (HttpURLConnection) url.openConnection();        // 提交模式conn.setRequestMethod("PUT");//POST GET PUT DELETE        //设置访问提交模式,表单提交conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");        conn.setConnectTimeout(10000);//连接超时 单位毫秒conn.setReadTimeout(2000);//读取超时 单位毫秒        conn.setDoOutput(true);// 是否输入参数StringBuffer params = new StringBuffer();// 表单参数与get形式一样params.append("customer").append("=").append(1);byte[] bypes = params.toString().getBytes();conn.getOutputStream().write(bypes);// 输入参数
//读取请求返回值InputStream inStream=conn.getInputStream();inStream.read(bypes, 0, inStream.available());System.out.println(new String(bypes, "gbk"));

原创粉丝点击