Java中通过方法创建一个http连接并请求(服务器间进行通信)
服务器间进行通信只能通过流(Stream)的方式进行,不能用方法的返回值。
1.Java代码创建一个连接并请求该连接返回的数据
doGet()方法,execute()方法中调用
package demo2.x.com; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import javax.print.attribute.standard.RequestingUserName; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /** * Demo2主页,访问主页要先验证cookie * * @author: qlq * @date : 2017年8月29日下午12:02:31 */ public class Demo2Action extends ActionSupport { private String gotoUrl; public String getGotoUrl() { return gotoUrl; } public void setGotoUrl(String gotoUrl) { this.gotoUrl = gotoUrl; } @Override public String execute() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); Cookie cookies[] = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if ("ssocookie".equals(cookie.getName())) { String result = this.doGet("http://check.x.com:8080/sso/checkCookie.action", cookie.getName(), cookie.getValue()); if ("1".equals(result)) { return SUCCESS; } } } } // 登陆失败后将gotoUrl写到JSP页面 gotoUrl = "http://demo2.x.com:8080/demo2/main.action"; return LOGIN; } public String doGet(String url, String cookieName, String cookieValue) { // 用于接收返回的数据 StringBuffer sb = new StringBuffer(); // 创建一个连接的请求 HttpURLConnection httpURLConnection = null; try { // 包装请求的地址 URL urls = new URL(url + "?cookieName=" + cookieName + "&cookieValue=" + cookieValue); // 打开连接 httpURLConnection = (HttpURLConnection) urls.openConnection(); httpURLConnection.setRequestMethod("GET"); // 通过BufferReader读取数据 InputStream iStream = httpURLConnection.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(iStream); BufferedReader bReader = new BufferedReader(inputStreamReader); String temp = null; while ((temp = bReader.readLine()) != null) { sb.append(temp); } // 关闭流(先开后关,后开先关) bReader.close(); inputStreamReader.close(); iStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (httpURLConnection != null) { // 关闭连接 httpURLConnection.disconnect(); } } return sb.toString(); } }
2.接收请求的连接
checkCookie()方法
package check.x.com; import java.awt.image.VolatileImage; import java.io.IOException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; import check.x.com.utils.CheckCookie; public class LoginAction extends ActionSupport { private String username; private String password; private String gotoUrl; private String cookieName; private String cookieValue; public String getCookieName() { return cookieName; } public void setCookieName(String cookieName) { this.cookieName = cookieName; } public String getCookieValue() { return cookieValue; } public void setCookieValue(String cookieValue) { this.cookieValue = cookieValue; } public String getGotoUrl() { return gotoUrl; } public void setGotoUrl(String gotoUrl) { this.gotoUrl = gotoUrl; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String execute() throws Exception { boolean OK = this.check(); if (OK) { Cookie cookie = new Cookie("ssocookie", "sso"); // 设置cookie的作用范围是父域(.x.com) cookie.setDomain(".x.com"); // 斜杠代表设置到父域的顶层,也就是父域下的所有应用都可访问 cookie.setPath("/"); HttpServletResponse response = ServletActionContext.getResponse(); // 增加cookie,未设置生命周期默认为一次会话 response.addCookie(cookie); return SUCCESS; } return null; } public void checkCookie() throws IOException{ String result="0"; if(CheckCookie.checkCookie(cookieName, cookieValue)){ result="1"; } HttpServletResponse response = ServletActionContext.getResponse(); response.getWriter().print(result); response.getWriter().close(); } public boolean check() { if ("user".equals(username) && "123".equals(password)) return true; return false; } }
简单的使用方法可以参考:
http://www.cnblogs.com/qlqwjy/p/7554535.html
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】
分类:
Java基础
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix