如何使用httpclient进行NTLM认证登录
来源:互联网 发布:免费发短信软件 编辑:程序博客网 时间:2024/06/10 07:16
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 0
- 如何使用httpclient进行NTLM认证登录
- 使用 Apache Jmeter 进行 NTLM 和 Kerberos 认证
- 如何使用HttpClient认证机制
- NTLM认证
- NTLM认证模块分发使用协议
- JCIFS NTLM HTTPAuthenticationJCIFS使用NTLMHTTP认证
- httpclient 实现自动登录NTLM 域验证(sso)
- 利用jcifs进行NTLM协议认证,生成ws访问客户端
- cocos2dx之如何使用HttpClient进行网络通讯
- HttpClient解决ntlm验证
- cognos8配置NTLM认证
- Microsoft NTLM认证简介
- NTLM认证过程
- python-ntlm认证
- NTLM 认证详解
- 使用httpclient模拟登录
- 使用httpclient模拟登录
- basic认证,digest认证,NTLM认证
- 解决loading descriptor for xxx 慢的问题
- Linux下安装go1.7.3
- spark性能优化:数据倾斜调优
- 树莓派sip视频电话-1:exosip2ctype+python使用硬件编解码
- 类加载-反射(JAVA灵魂)
- 如何使用httpclient进行NTLM认证登录
- Linux : E: Unable to locate package XXX
- 为什么要有linked list?
- ~/.bash_profile 与~/.bashrc,以及http_proxy,http302
- Android ImageView 正确使用姿势
- JavaGUI
- ural 1820. Ural Steaks -思维题
- 移动端自适应布局----rem字体
- 静态库和共享库