25,980
社区成员
发帖
与我相关
我的任务
分享#!/usr/bin/perl -w
use LWP;
use HTTP::Cookies;
use warnings;
sub login{
($username,$pwd)=@_;
my ($url,$res);
my $ua = LWP::UserAgent->new;
my $cookie_jar=HTTP::Cookies->new(file =>"./cookie.taobao",ssl_opts =>{verify_hostname=>0},autosave => 1);
# get cookie
$url='https://login.taobao.com/member/login.jhtml';
$ua->cookie_jar($cookie_jar);
$ua->agent("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; User-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1); .NET CLR 2.0.50727)");
#$res = $ua->get($url);
# post form
$ua->cookie_jar($cookie_jar);
push @{$ua->requests_redirectable}, 'POST';
$res = $ua->post(
$url,
[
TPL_username => $username,
TPL_password => $pwd,
actionForStable => 'enable_post_user_action',
action => 'Authenticator',
event_submit_do_login => 'anything',
]
);
#print $res->content();
if($res->content()=~/class=\"error\"><\/p>/){
print "登陆成功!\n";
}else{
print "登陆失败!\n";
}
}
login("username","password");

package pdm.tool;
import java.util.HashMap;
import java.util.Map;
public class Cookie {
public native static String getCookie(String url);
static {
try {
System.loadLibrary("Cookie");
}
catch(UnsatisfiedLinkError e) {
e.printStackTrace();
}
}
public static void printCookie(String url) {
String cookie = getCookie(url);
if(cookie.length() > 0) {
String[] cookies = cookie.split("; ");
for(int i = 0; i < cookies.length; i++) {
System.err.println(cookies[i]);
}
}
else {
System.err.println("not found cookie of " + url);
}
}
public static Map<String, String> getCookieMap(String url) throws Exception {
String cookie = getCookie(url);
Map<String, String> cookieMap = new HashMap<String, String>();
if(cookie != null && cookie.trim().length() > 0) {
String[] cookies = cookie.split("; ");
for(int i = 0; i < cookies.length; i++) {
String name = cookies[i].split("=", 2)[0];
String value = cookies[i].split("=", 2)[1];
cookieMap.put(name, value);
}
}
else {
throw new Exception("not.found.partminer.cookie");
}
return cookieMap;
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
printCookie("http://www.baidu.com");
getCookieMap("http://www.baidu.com");
}
}
JNIEXPORT jstring JNICALL Java_pdm_tool_Cookie_getCookie
(JNIEnv *env, jclass, jstring jurl)
{
const char *url = env->GetStringUTFChars(jurl, 0);
TCHAR buffer [16384];
DWORD bufferSize = sizeof(buffer)/sizeof(buffer[0]);
BOOL bRes = InternetGetCookie(url, "", buffer, &bufferSize);
env->ReleaseStringUTFChars(jurl, url);
jstring jval = NULL;
if(bRes) {
jval = env->NewStringUTF(buffer);
}
else
{
jval = env->NewStringUTF("");
}
return jval;
}