后端发起POST请求

创建RedirectWithPost工具类


public class RedirectWithPost {

	Map<String, String> parameter = new HashMap<String, String>();
	HttpServletResponse response;

	public RedirectWithPost(HttpServletResponse response) {
		this.response = response;
	}

	public void setParameter(String key, String value) {
		this.parameter.put(key, value);
	}

	public void sendByPost(String url) throws IOException {
		this.response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = this.response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println(" <HEAD>");
		out.println(" <meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
		out.println(" <TITLE>loading</TITLE>");
		out.println(" <meta http-equiv=\"Content-Type\" content=\"text/html charset=GBK\">\n");
		out.println(" </HEAD>");
		out.println(" <BODY>");
		out.println("<form name=\"submitForm\" action=\"" + url + "\" method=\"post\">");
		Iterator<String> it = this.parameter.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			out.println("<input type=\"hidden\" name=\"" + key + "\" value=\"" + this.parameter.get(key) + "\"/>");
		}
		out.println("</from>");
		out.println("<script>window.document.submitForm.submit();</script> ");
		out.println(" </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

}

发起请求

	/**
	 * 问题校验 实际登录
	 * 
	 * @param response
	 * @throws IOException
	 */
	@PostMapping("/problem_authentication")
	public void ckeck(HttpServletResponse response) throws IOException {

		SysUser user = (SysUser) session.getAttribute("loginSysUser");
		if (user == null) {
			response.sendRedirect("/login");
		} else {
			/**
			 * 验证问题
			 */
			

			/**
			 * 实际登录
			 */
			RedirectWithPost redirectWithPost = new RedirectWithPost(response);
			// redirectUrl请求地址
			String redirectUrl = "/to_login";
			redirectWithPost.setParameter("username", user.getUsername());
			redirectWithPost.setParameter("password", user.getPassword());
			redirectWithPost.sendByPost(redirectUrl);
		
		}

	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java后端发起POST请求可以使用Java提供的HttpURLConnection类或者第三方库如Apache的HttpClient等。 下面是使用HttpURLConnection类发起POST请求的示例代码: ```java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpPostExample { public static void main(String[] args) throws Exception { String url = "http://example.com/api/endpoint"; // 请求的URL URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 设置请求方法为POST con.setRequestMethod("POST"); // 设置请求头信息 con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Content-Type", "application/json"); // 设置POST请求参数 String requestBody = "{\"key\": \"value\"}"; con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(requestBody); wr.flush(); wr.close(); // 发起请求并获取响应 int responseCode = con.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 输出响应结果 System.out.println("Response Code : " + responseCode); System.out.println("Response : " + response.toString()); } } ``` 其中,必须设置请求方法为POST,设置Content-Type为application/json等需要根据实际情况进行调整。 另外,如果需要进行HTTPS请求,需要额外设置SSL证书验证等操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值