1. 程式人生 > >Java中通過方法創建一個http連接並請求

Java中通過方法創建一個http連接並請求

servlet 讀取 mage equals contex method ade temp password

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;
    }

}

Java中通過方法創建一個http連接並請求