如何使用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);

    }



}

你可能感兴趣的:(java)