zoukankan      html  css  js  c++  java
  • Java调用http保留访问状态

    package com.coracle;
    
    import com.coracle.yk.xframework.util.yunTongXun.HttpRequest;
    
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    /**
     * Created by huangbaidong
     * 2017/4/12.
     */
    public class TestHttpStatus {
    
        public static void main(String[] args) throws Exception {
            try {
                //1、先登录获取SessionId也就是cookie
                String sessionId = login();
                //2、然后调用其他接口,带上cookie,
                String result = httpRequest("http://localhost:8083/xweb/api/v2/order/list", "{}", sessionId);
                System.out.println(result);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        /**
         * 访问其他有权限控制的接口
         * @param url  自己传URL
         * @param json
         * @param sessionId
         * @return
         * @throws Exception
         */
        public static String httpRequest(String url, String json, String sessionId) throws Exception {
            // 使用POST方式向目的服务器发送请求
            URL connect;
            OutputStreamWriter paramout = null;
            BufferedReader reader = null;
    
            StringBuffer data = new StringBuffer();
            try {
                connect = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) connect.openConnection();
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
                connection.setRequestProperty("Content-Type", "application/json");
                connection.setRequestProperty("Cookie", sessionId);
                System.out.println("newSession:"+sessionId);
                paramout = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
                paramout.write(json);
                paramout.flush();
    
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
                String line;
                while ((line = reader.readLine()) != null) {
                    data.append(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (paramout != null) {
                        paramout.close();
                    }
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            return data.toString();
        }
    
    
        /**
         * 登录获取session
         * @return
         * @throws IOException
         */
        public static String login() throws IOException {
            URL url = new URL("http://localhost:8083/xweb/api/v2/login/userLogin?loginName=positec&password=qwe123");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json");
            PrintWriter out = new PrintWriter(conn.getOutputStream());
            String str = "url = " + url;
            out.println(str);
            out.flush();
            BufferedReader in = null;
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String destStr = "";
            String inputLin = "";
            while((inputLin = in.readLine()) != null ){
                destStr +=  inputLin;
            }
            System.out.println(destStr);
            String session_value = conn.getHeaderField("Set-Cookie");
            String[] sessionId = session_value.split(";");//第一登录,取出SESSIONID
            System.out.println("Session Value = " + session_value);
            return sessionId[0];
        }
    }
  • 相关阅读:
    【URAL 1004】 floyd最小环
    【UVA 10881】 经典模拟题
    【HDU 1541】 树状数组(入门题)
    【HDU 4000】 树状数组
    【HDU 3391 && HDU 4431】 dfs+模拟
    【HDU 1058 & HDU 3199 类似丑数】 简单DP思想
    Acdream原创群赛3(部分题解)
    vfor实现双层循环嵌套
    vue获取当前时间并实时刷新时间
    vue+element ui实现左侧导航栏动态路由跳转
  • 原文地址:https://www.cnblogs.com/cocoat/p/6773085.html
Copyright © 2011-2022 走看看