1. 程式人生 > >HTTP + Connection + Cookie登入後訪問其他介面獲取JSON資料

HTTP + Connection + Cookie登入後訪問其他介面獲取JSON資料

SAPHttpTest.java

package com.aac.rabbitmqdemo2.http;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class SAPHttpTest {

    private static final String LOGIN_URL = "http://bpmtest.aact.com/console/user/login.action";


    /**
     * 獲取登入cookie:JSESSIONID及其對應的值
     * @return
     */
    private static String getCookieVal() {
        String cookieVal = null;
        try {
            URL url = new URL(LOGIN_URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
            out.write("username=admin&password=123");
            out.flush();
            out.close();
            cookieVal = connection.getHeaderField("Set-Cookie");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cookieVal;
    }

    /**
     * POST方法訪問HTTP介面
     * @param post_url
     * @param paramStr
     */
    public static void visitByPost (String post_url, String paramStr) {
        try {
            URL url = new URL(post_url);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            String cookieVal = getCookieVal();
            if (cookieVal != null) {
//                String loginCookie = cookieVal.split(";", 2)[0]; // 通過getCookieVal方法獲取的不能登入
                String loginCookie = "JSESSIONID=0000KDoR2_0URHYGKDbFFnLvvbZ:-1"; // postman獲取的可以登入
                connection.setRequestProperty("Cookie", loginCookie);
            }
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            connection.connect();
            DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
            dataout.writeBytes(paramStr);
            dataout.flush();
            dataout.close();
            BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = bf.readLine()) != null) {
                sb.append(line).append(System.getProperty("line.separator"));
            }
            bf.close();
            connection.disconnect();
            System.out.println(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

RabbitmqDemo2ApplicationTests.java

package com.aac.rabbitmqdemo2;

import com.aac.rabbitmqdemo2.http.SAPHttpTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqDemo2ApplicationTests {

    @Test
    public void testFixingsInfo() {
        String params = "pid=1&pname=螺絲";
        SAPHttpTest.visitByPost("http://localhost:8080/erp/product/info", params);
    }

    @Test
    public void contextLoads() {
    }

}