HttpClient ,java模拟http/login,获取sessionId,跳过登录

模拟登录

  • 无需导入jar包,tomcat自带包,简单易学
  • 代码

无需导入jar包,tomcat自带包,简单易学

代码

package com.datatech.app.common.util;
import java.io.IOException;

import javax.servlet.http.HttpUtils;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class Test {
    public static void main(String[] args) throws IOException {
    	String loginUrl = "http://192.168.1.92:12345/datastudio/login";
        // 测试 jobhistory 接口
//        String dataUrl = "http://47.111.207.139:8489/api/rest_j/v1/jobhistory/125/get";
//        String dataUrl2 = "http://47.111.207.139:8489/api/rest_j/v1/configuration/getFullTreesByAppName";
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(loginUrl);
        //普通的参数格式
        NameValuePair[] data = { new NameValuePair("userName", "admin"), new NameValuePair("userPassword", "dataworks123") };
        postMethod.setRequestBody(data);
        //参数以json格式
        //注意!!!!,我之前用json格式没通过用的普通参数格式
//        RequestEntity requestEntity=new StringRequestEntity("{\"userName\":\"admin\",\"userPassword\":\"dataworks123\"}");
//        postMethod.setRequestEntity(requestEntity);
//        Part[] parts = {new StringPart("userName", "admin"),
//                                new StringPart("password", "admin")};
//        postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
        // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
        httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        int statusCode=httpClient.executeMethod(postMethod);
        // 获得登陆后的 Cookie
        Cookie[] cookies = httpClient.getState().getCookies();
        StringBuffer tmpcookies = new StringBuffer();
        System.out.println(postMethod.getResponseBodyAsString());
        for (Cookie c : cookies) {
            tmpcookies.append(c.toString() + ";");
            System.out.println("cookies = "+c.toString());
        
            String dataUrl= "http://127.0.0.1:8888/#/home";
             // 进行登陆后的操作
//	        GetMethod getMethod = new GetMethod(dataUrl);
//	        // 每次访问需授权的网址时需带上前面的 cookie 作为通行证
//	        String headerValue = tmpcookies.toString();
//            getMethod.setRequestHeader("cookie", headerValue);
//        	// 你还可以通过 PostMethod/GetMethod 设置更多的请求后数据
//            // 例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外
//            postMethod.setRequestHeader("Referer", "http://47.111.207.139:8489/");
//            postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36");
//            httpClient.executeMethod(getMethod);
            // 打印出返回数据
//            String text = getMethod.getResponseBodyAsString();
//            System.out.println(text);
//	        String s = HttpUtils.doPostJson(dataUrl, headerValue, "{\"globalVariables\":[{\"key\":\"foo\",\"value\":\"bar\"}]}");
//	        System.out.println(s);
        }
        
//    }
    }
}

你可能感兴趣的:(小知识,java,http,tomcat)