我正在尝试以编程方式登录mobile.twitter.com。以下是手动执行的步骤: 1)转到https://mobile.twitter.com/session/new
This has a web form of structure:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.1//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="HandheldFriendly" content="True"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<meta name="twitter-redirect-srcs" content="{"pwreset-iphone":true,"android":true,"email":true}">
<meta name="twitter-redirect-url" content="twitter://timeline">
<link href="https://ma-0.twimg.com/twitter-mobile/9635f415e4f7c5dce70ba32d2e0b334893f16c7f/images/ic_favicon.png" rel="icon" type="image/png" />
<title>Sign in to Twitter!</title>
<link href="https://ma-0.twimg.com/twitter-mobile/9635f415e4f7c5dce70ba32d2e0b334893f16c7f/assets/m2s.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body class="images nojs sessions-page sessions-new-page">
<div id="container">
<div id="brand_bar">
<table id="top">
<tr>
<td class="modal-left">
<a href="https://mobile.twitter.com/" class="brandmark"><img alt="Twitter" height="28" src="https://ma-0.twimg.com/twitter-mobile/9635f415e4f7c5dce70ba32d2e0b334893f16c7f/images/sprites/larry_28px.gif" /></a>
<span class="title">Sign in to Twitter!</span>
</td>
<td class="modal-right">
<a href="/signup" class="w-button-common w-button-bright">Sign up</a>
</td>
</tr>
</table>
</div>
<div id="main_content">
<div class="header">
<h2>Sign in</h2>
</div>
<div class="body">
<form action="https://mobile.twitter.com/session" method="post">
<span class="m2-auth-token"><input name="authenticity_token" type="hidden" value="2a59e713a2fba05f4ac9" /></span>
<fieldset class="inputs">
<label for="username">Username</label>
<div class="input-wrapper">
<input autocapitalize="off" autocorrect="off" class="text-input" name="username" id="username" placeholder="" type="text" value="">
</div>
<div class="hint">You can also use your email address</div>
<label for="password">Password</label>
<div class="input-wrapper">
<input autocomplete="off" class="text-input" name="password" id="password" placeholder="" type="password" value="">
</div>
</fieldset>
<span class="w-button-common w-button-bright"><input name="commit" type="submit" value="Sign in" /></span>
</form>
</div>
</div>
<div id="footer">
<div class="signup">
<a href="/account/resend_password">Forgot password?</a>
</div>
<div class="signup">
<a href="/signup/sms">Already using Twitter via SMS?</a>
</div>
</div>
</div>
</body>
</html>
所以有4个输入参数:用户名,密码,access_token和提交。表单的操作网址为https://mobile.twitter.com/session
以下是我尝试登录的代码:
String url = "https://mobile.twitter.com/session/new";
String url2 = "https://mobile.twitter.com/session";
DefaultHttpClient httpclient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
String formCode = EntityUtils.toString(entity);
Map<String,String> formParams =getFormParams(formCode, "myusername", "mypwd");
// make sure cookies is turn on
CookieHandler.setDefault(new CookieManager());
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
HttpPost httpost = new HttpPost(url2);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
for(String s:formParams.keySet()){
nvps.add(new BasicNameValuePair(s, formParams.get(s)));
}
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost,httpContext);
entity = response.getEntity();
formCode = EntityUtils.toString(entity);
Log.d("FormCode",formCode);
(请注意,在上面的代码中,getFormParams返回正确的映射,即具有4个名称值对的映射)。 我正在从android执行此代码。当我在Log.debug中看到FormCode时,我总是得到相同的主页。两种情况下的response.getStatusLine()。getStatusCode()返回200.那么为什么我总是登陆登录页面?我已经花了一个多星期来解决这个问题。我迫切需要一些帮助......
答案 0 :(得分:0)
我不能在中国使用Twitter。但我可以告诉你我登录WebChat的代码。 我认为他们将是一样的。关键是你每次都使用相同的客户端和上下文。标题应该设置合适。 我将clint和context设置为静态,所以我可以随时随地使用它们,这只是为了简单。
public static HttpClient client;
public static HttpContext context;
//登陆
static{
String username = "ewallet2014@qq.com";
String password = "xxxxxxxx";
try {
dologin(username, password);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void dologin(String name, String passwd) throws Exception {
// TODO Auto-generated method stub
client = new DefaultHttpClient();
context = new BasicHttpContext();
HttpPost post = new HttpPost(
"https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", name));
params.add(new BasicNameValuePair("pwd", passwd));
params.add(new BasicNameValuePair("f", "json"));
post
.setHeader("Accept-Language",
"zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4");
post
.setHeader("accept",
"application/json, text/javascript, */*; q=0.01");
post
.setHeader(
"user-agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
post.setHeader("Origin","https://mp.weixin.qq.com");
post.setHeader("Referer","https://mp.weixin.qq.com/");
post.setHeader("Origin","https://mp.weixin.qq.com");
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response = client.execute(post, context);
int stat = response.getStatusLine().getStatusCode();
//System.out.println(stat);
if (stat == 200) {
String str = EntityUtils.toString(response.getEntity());
System.out.println(str);
//token = StringUtils.substringBetween(str, "token=", "\"}");
System.out.println("login success! ");
}
EntityUtils.consume(response.getEntity());
}