java 发送http请求(提交form表单)
2013-12-04 10:29
519 查看
写一个servlet用于测试请求
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Abc extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doGet"); this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doPost"); System.out.println("aaa : "+request.getParameter("aaa")); System.out.println("param1 : "+request.getParameter("param1")); System.out.println(request.getHeader("avc")); PrintWriter pw = response.getWriter(); pw.write(request.getParameter("param1")); pw.flush(); pw.close(); } }
web.xml配置文件
<servlet> <description></description> <display-name>Abc</display-name> <servlet-name>Abc</servlet-name> <servlet-class>com.Abc</servlet-class> </servlet> <servlet-mapping> <servlet-name>Abc</servlet-name> <url-pattern>/Abc</url-pattern> </servlet-mapping>
抽象出一个工具请求类
package http; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; import java.util.Map.Entry; public class HttpUtil { public static String http(String url, Map<String, String> params) { URL u = null; HttpURLConnection con = null; // 构建请求参数 StringBuffer sb = new StringBuffer(); if (params != null) { for (Entry<String, String> e : params.entrySet()) { sb.append(e.getKey()); sb.append("="); sb.append(e.getValue()); sb.append("&"); } sb.substring(0, sb.length() - 1); } System.out.println("send_url:" + url); System.out.println("send_data:" + sb.toString()); // 尝试发送请求 try { u = new URL(url); con = (HttpURLConnection) u.openConnection(); //// POST 只能为大写,严格限制,post会不识别 con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8"); osw.write(sb.toString()); osw.flush(); osw.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { con.disconnect(); } } // 读取返回内容 StringBuffer buffer = new StringBuffer(); try { //一定要有返回值,否则无法把请求发送给server端。 BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); String temp; while ((temp = br.readLine()) != null) { buffer.append(temp); buffer.append("\n"); } } catch (Exception e) { e.printStackTrace(); } return buffer.toString(); } }
备注:一定要获取返回值,否则请求无法到达。
readLine()为阻塞方法,会等待服务器的响应,否则会一直等待
con.setDoOutput(true); 这个表示是要发送请求到URL地址,没有这句话server是不会响应的。
参数的传递方式类似于在URL后面添加"?key=value&key1=value1"这种方式
测试类
@Test public void test3() { Map parames = new HashMap<String, String>(); parames.put("param1", "param1_value"); parames.put("param2", "param2_value"); parames.put("param3", "param3_value"); // 如果地址栏中有aaa这个参数,则默认选择地址栏的,如果没有则选择添加的参数 parames.put("aaa", "aaa_value"); HttpUtil.http("http://localhost:8080/a/Abc?aaa=dddd", parames); }
相关文章推荐
- 解决Error:ftl页面通过form提交表单,后台java类接收的值为null
- java中multipart/form-data方式提交表单添加验证问题
- java模拟form表单提交图片文件
- 【Java】form表单提交中文乱码
- HttpClient:模拟表单提交文件,发送multipart/form-data数据
- Form表单提交,Ajax请求,$http请求的区别
- asp.net mvc4 使用java异步提交form表单时出现[object object] has no method ajaxSubmit
- java发起http请求提交表单
- Ajax应用serialize()提交Form表单及后端取值(Java)
- form 表单提交 拼接url发送到后台
- java springboot上传图片form表单提交上传
- 【Java】向一个页面发送post请求并获取其处理之后的结果与用javascript创建一个表单后提交的安全性
- java模拟form表单提交图片文件Posted by
- 用java模拟html表单(form)提交数据包含多文件上传表单提交。
- Java发送Http请求,模拟表单上传文件
- 突然,form表单参数提交,但是java接收不到
- Java ajax servlet 无法接收到 jsp 以post提交的 form 表单信息 可能的解决方案(都是工具惹得祸!)
- 模拟以Form表单方式发送Http请求
- java仿form表单提交数据
- FORM表单提交,后端JAVA获取文件名称