1. 程式人生 > >如何使用httpclient進行NTLM認證登入

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