如何使用httpclient进行NTLM认证登录

NTLM是微软的一种安全认证机制,有些网站是实用NTLM做的认证登陆,使用httpclient认证后可以发送一些get,post请求。代码是用来自动签到的,重点在于ntml认证。

package com.meican.service;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;



import org.apache.http.HttpEntity;

import org.apache.http.HttpHost;

import org.apache.http.HttpResponse;

import org.apache.http.auth.AuthScope;

import org.apache.http.auth.NTCredentials;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.protocol.BasicHttpContext;

import org.apache.http.protocol.HttpContext;



public class Login {



    public static String foldName = "";

    public static String userName = "";

    public static String password = "";

    public static void main(String[] args) throws Exception {

        punck(userName,password);

    }

    public static void punck(String userName,String password){

        setMoningSchdule();

        setNoonSchdule();

        setNightSchdule();

    }

    public static void setMoningSchdule(){

        Timer timer = new Timer();

        timer.schedule(new TimerTask() {



            @Override

            public void run() {

                try {

                    postRequest(userName, password,

                            url);



                    writeLog("上午签到返回成功");

                }catch (Exception e) {



                    writeLog("上午签到返回失败"+e.getMessage());

                }

                //明天的定时器

                setMoningSchdule();

            }

        }, getMoningDate());

    }



    public static void setNoonSchdule(){

        Timer timer = new Timer();

        timer.schedule(new TimerTask() {



            @Override

            public void run() {

                try {

                    postRequest(userName, password,

                            "url1");

                    long d = (long) (Math.random()*120000);

                    Thread.sleep(d);

                    postRequest(userName, password,

                            "url2");

                    writeLog("中午午签到返回成功");

                }catch (Exception e) {



                    writeLog("中午签到返回失败"+e.getMessage());

                }

                //明天的定时器

                setNoonSchdule();

            }

        }, getNoonDate());

    }



    public static void setNightSchdule(){

        Timer timer = new Timer();

        timer.schedule(new TimerTask() {



            @Override

            public void run() {

                try {



                    postRequest(userName, password,

                            "url4");

                    writeLog("下班签到返回成功");

                }catch (Exception e) {



                    writeLog("下班签到返回失败"+e.getMessage());

                }

                //明天的定时器

                setNightSchdule();

            }

        }, getNightDate());

    }

    public static Date getMoningDate(){

        Date date = new Date();

        date.setDate(date.getDate()+1);

        date.setHours(8);

        date.setSeconds((int) (Math.random()*60));

        long minute = (long) (Math.random()*30);

        date.setMinutes((int) (minute+30));

        writeLog("明天早晨打卡时间"+printDate(date));

        return date;

    }

    public static Date getNoonDate(){

        Date date = new Date();

        date.setDate(date.getDate()+1);

        date.setHours(11);

        date.setSeconds((int) (Math.random()*60));

        long minute = (long) (Math.random()*30);

        date.setMinutes((int) (minute+60));

        writeLog("明天中午打卡时间"+printDate(date));

        return date;

    }

    public static Date getNightDate(){

        Date date = new Date();

        date.setDate(date.getDate()+1);

        date.setHours(18);

        date.setSeconds((int) (Math.random()*60));

        long minute = (long) (Math.random()*30);

        date.setMinutes((int) (minute));



        writeLog("明天晚上打卡时间"+printDate(date));

        return date;

    }

    public static String printDate(Date date){



        int day = date.getDay();

        int h = date.getHours();

        int m = date.getMinutes();

        int s = date.getSeconds();

        int dm = date.getDate();

        int mon = date.getMonth();

        int year = 1900 + date.getYear();

        String dateStr = year + "-" + (mon+1) + "-"+ dm;

        String timeStr = dateStr + " " + h + ":" + m + ":" + s;

        return timeStr;

    }

    @SuppressWarnings("deprecation")

    public static void postRequest(String userName, String password, String url)

            throws ClientProtocolException, IOException {

        Date date = new Date();

        if(date.getDay() ==0 || date.getDay() == 6){

            writeLog("今天是周末");

            return;

        }

        writeLog(url);

        DefaultHttpClient httpclient = new DefaultHttpClient();

        NTCredentials creds = new NTCredentials(userName, password,

                "", "");



        httpclient.getCredentialsProvider()

                .setCredentials(AuthScope.ANY, creds);

        HttpHost target = new HttpHost("172.19.10.4", 80, "http");

        // 保证相同的内容来用于执行逻辑相关的请求

        HttpContext localContext = new BasicHttpContext();

        // 首先执行简便的方法。这会触发NTLM认证

        // 之后使用相同的内容(和连接)执行开销大的方法。

        HttpPost httppost = new HttpPost(url);

        httppost.setEntity(new StringEntity("lots and lots of data"));

        HttpResponse response2 = httpclient.execute(target, httppost,

                localContext);

        HttpEntity entity2 = response2.getEntity();

        try{

            InputStream inSm = entity2.getContent();



            BufferedReader br = new BufferedReader(new InputStreamReader(inSm,

                    "GB2312"));

            String data = "";

            while ((data = br.readLine()) != null) {

                writeLog(data);

            }

        }catch(Exception e){

        }



    }



    public static void writeLog(String message) {

        System.out.println(message);

    }



}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
HttpClient是一个常用的HTTP客户端工具,用于发送HTTP请求和接收HTTP响应。在进行OAuth认证时,可以使用HttpClient来实现。 首先,需要导入HttpClient的相关包,并创建一个HttpClient实例。可以使用以下代码创建一个默认的HttpClient实例: ``` CloseableHttpClient httpClient = HttpClients.createDefault(); ``` 接下来,需要构建OAuth认证请求。OAuth认证通常涉及授权码的获取、令牌的申请和刷新等步骤。可以使用HttpClient的HttpPost或HttpGet类来构建相应的请求。例如,使用HttpPost构建一个获取授权码的请求: ``` HttpPost request = new HttpPost("https://oauth.example.com/authorize"); request.setHeader("Content-Type", "application/x-www-form-urlencoded"); request.addParameter("client_id", "your_client_id"); request.addParameter("redirect_uri", "your_redirect_uri"); request.addParameter("response_type", "code"); ``` 然后,发送认证请求并接收响应。可以使用HttpClient的execute方法发送请求并接收响应。例如: ``` CloseableHttpResponse response = httpClient.execute(request); ``` 接收到响应后,可以从响应中获取认证相关的信息。例如,可以从响应的实体中获取认证授权码: ``` HttpEntity entity = response.getEntity(); String authCode = EntityUtils.toString(entity); ``` 接下来的步骤根据具体的OAuth流程会有所不同,可以根据实际需求进行令牌的申请、刷新等操作。 需要注意的是,在进行OAuth认证时,可能会涉及到授权头信息等安全相关的操作。可以通过HttpClient的方法设置请求头信息来确保安全性。 总之,使用HttpClient进行OAuth认证需要创建HttpClient实例、构建认证请求、发送请求并接收响应,然后根据具体的OAuth流程进行相应的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱美事爱生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值