java发送http请求(提交form表单)
Posted Bob_C
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java发送http请求(提交form表单)相关的知识,希望对你有一定的参考价值。
http://hbiao68.iteye.com/blog/1973914
写一个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);
- }
以上是关于java发送http请求(提交form表单)的主要内容,如果未能解决你的问题,请参考以下文章