httpclient 登录新浪微博成功 为何发不了 微博 急求??
登录发发微薄 类
private Log log = LogFactory.getLog(Sina.class);
/**
* @param args
*/
public static void main(String[] args) {
W_self_accounts w_user = new W_self_accounts();
w_user.setW_useruame_sl("qq312238436");
w_user.setW_password("201314");
Sina sina = new Sina();
sina.login(w_user);
}
/**
* 登录验证
*
* @param weibuserinfo
*/
private String ssoLogin(W_self_accounts weibuserinfo, String chkCode) {
// 获取server time nice
log.info("ssoLogin");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new NameValuePair("useticket", "1"));
nvps.add(new NameValuePair("ssosimplelogin", "1"));
nvps.add(new NameValuePair("su", encodeAccount(weibuserinfo.getW_useruame_sl())));
nvps.add(new NameValuePair("service", "miniblog"));
nvps.add(new NameValuePair("servertime", weibuserinfo.getServerTime()));
nvps.add(new NameValuePair("nonce", weibuserinfo.getNoce()));
nvps.add(new NameValuePair("pwencode", "wsse"));
nvps.add(new NameValuePair("sp", new SinaSSOEncoder().encode(weibuserinfo.getW_password(), weibuserinfo.getServerTime(), weibuserinfo.getNoce())));
nvps.add(new NameValuePair("url", "http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack"));
nvps.add(new NameValuePair("returntype", "META"));
nvps.add(new NameValuePair("encoding", "UTF-8"));
nvps.add(new NameValuePair("client=", "ssologin.js(v1.3.19)"));
// 验证码
nvps.add(new NameValuePair("vsnval", chkCode));
nvps.add(new NameValuePair("entry", "weibo"));
nvps.add(new NameValuePair("gateway", "1"));
nvps.add(new NameValuePair("from", ""));
nvps.add(new NameValuePair("savestate", "7"));
nvps.add(new NameValuePair("useticket", "1"));
nvps.add(new NameValuePair("ssosimplelogin", "1"));
nvps.add(new NameValuePair("returntype", "META"));
nvps.add(new NameValuePair("encoding", "UTF-8"));
// ?client=ssologin.js(v1.3.19)
String entity = WebRuquest.post("http://login.sina.com.cn/sso/login.php", nvps, "");
return entity;
}
public void login(W_self_accounts weibuserinfo) {
// 获取登录需要的参数
preLogin(weibuserinfo);
// 登录
String entity = ssoLogin(weibuserinfo, "");
if (StringUtils.isBlank(entity) || StringUtils.isEmpty(entity)) {
log.info(weibuserinfo.getW_useruame_sl() + "登录失败,请检查网络");
return;
}
if (entity.indexOf("retcode=0") != -1) {// 登录成功
ajaxLogin(entity, weibuserinfo);
home(weibuserinfo);
} else if (entity.indexOf("retcode=4049") != -1) {
log.info(weibuserinfo.getW_useruame_sl() + "需要验证");
ImageDialog dialog = new ImageDialog();
// while (true) {
// String img = "http://login.sina.com.cn/cgi/pin.php?r=" + (new
// Random().nextInt()) + "&s=0&p=" + weibuserinfo.getPcid();
// dialog.setUrl(img);
// dialog.setUserName(weibuserinfo.getW_useruame_sl());
// dialog.setVisible(true);
// dialog.show();
// // 此句会阻塞
// System.out.println("测试 阻塞000000000 +=========");
// String code = dialog.getCode();
// System.out.println("测试 阻塞 1111111+=========code:" + code);
// String chkCode = "&pcid=" + weibuserinfo.getPcid() + "&door=" +
// code;
// double serverTime =
// Double.parseDouble(weibuserinfo.getServerTime()) + 40.0;
// String nonce = makeNonce(6);
// weibuserinfo.setNoce(nonce);
// weibuserinfo.setServerTime(serverTime + "");
// entity = ssoLogin(weibuserinfo, chkCode);
// System.out.println("++++++++++++++++++++");
// System.out.println(entity);
// System.out.println("++++++++++++++++++++");
// dialog.isOk = false;
// if (entity.indexOf("retcode=0") != -1) {
// log.info("break");
// break;
// }
// }
} else {
ajaxLogin(entity, weibuserinfo);
}
}
/**
* 获取登录所需要参数 获取 servertime,nonce
*
* @param weibUserInfo
*/
private void preLogin(W_self_accounts weibUserInfo) {
log.info("preLogin");
String loginName = weibUserInfo.getW_useruame_sl();
String encodeName;
try {
encodeName = URLEncoder.encode(encodeAccount(loginName), "utf-8");
} catch (UnsupportedEncodingException e) {
encodeName = encodeAccount(loginName);
}
String url = "http://login.sina.com.cn/sso/prelogin.php?entry=miniblog&callback=sinaSSOController.preloginCallBack";
url += "&user=" + encodeName;
url += "&client=ssologin.js(v1.3.16)";
String requestHtml = WebRuquest.post(url, null, "");
log.info(requestHtml);
weibUserInfo.setServerTime(JsonUtils.getKey(requestHtml, "servertime"));
weibUserInfo.setNoce(JsonUtils.getKey(requestHtml, "nonce"));
weibUserInfo.setPcid(JsonUtils.getKey(requestHtml, "pcid"));
}
/**
* 此次为最后一次登录用来获取cookie
*
* @param weibUserInfo
*/
private void ajaxLogin(String entity, W_self_accounts weibUserInfo) {
log.info("ajaxLogin");
entity = entity.substring(entity.indexOf("location.replace(") + "location.replace(".length() + 1);
String url = entity.substring(0, entity.indexOf(");") - 1);
entity = WebRuquest.getOfCookie(url, weibUserInfo);
if (entity.indexOf("CallBack({") != -1) {
if ("true".equals(JsonUtils.getKey(entity, "result"))) {
System.out.println(JsonUtils.getKey(entity, "userinfo"));
JSONObject userinfo = JSONObject.fromObject(JsonUtils.getKey(entity, "userinfo"));
weibUserInfo.setW_displayname(userinfo.optString("displayname", ""));
log.info(weibUserInfo.getW_displayname());
log.info(userinfo.optString("displayname", ""));
weibUserInfo.setW_userdomain(userinfo.optString("userdomain", ""));
weibUserInfo.setW_account_id(userinfo.optString("uniqueid", ""));
weibUserInfo.setW_useruame_sl(userinfo.optString("userid", ""));
CacheUser.Add(weibUserInfo);
sendWeibo(weibUserInfo);
} else {
log.info("帐号:" + weibUserInfo.getW_useruame_sl() + JsonUtils.getKey(entity, "reason"));
}
}
}
private void home(W_self_accounts weibUserInfo) {
log.info("home");
String url = "http://www.weibo.com/u/";
url += weibUserInfo.getW_account_id() + "?" + weibUserInfo.getW_userdomain();
String html = WebRuquest.get(url, weibUserInfo);
if (null == html) {
log.info("网络异常,无法访问url");
} else {
if (html.indexOf("你当前使用的帐号异常,请完成手机验证,提升帐号安全") != -1) {
log.info("你当前使用的帐号异常,请完成手机验证,提升帐号安全");
} else {
WeiBoHeper.getInstane().UpdateUserInfo(weibUserInfo, html);
}
}
}
private void sendWeibo(W_self_accounts user) {
log.info("sdfdf");
// List<org.apache.commons.httpclient.Cookie> wbCookie =
// userlist.get(i).cookies;
String url = "http://picupload.service.weibo.com/interface/pic_upload.php?cb=";
String referer = "http://weibo.com/u/" + user.getW_account_id();
String str3 = "";
url = "http://weibo.com/aj/mblog/add?__rnd=" + RandomUtils.nextInt();
String str6 = "", oldContent = "";
String sendMsg = " 程序test 微博!!!!!!!";
if (null != sendMsg) {
try {
str6 = URLEncoder.encode(sendMsg, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
oldContent = sendMsg;
}
// String requestParam = "_t=0&location=home&module=stissue&pic_id="
// + str3 + "&text=" + str6;
List<NameValuePair> paramlist = new ArrayList<NameValuePair>();
paramlist.add(new NameValuePair("_t", "0"));
paramlist.add(new NameValuePair("location", "home"));
paramlist.add(new NameValuePair("module", "stissue"));
paramlist.add(new NameValuePair("pic_id", str3));
paramlist.add(new NameValuePair("text", str6));
paramlist.add(new NameValuePair("_surl", ""));
paramlist.add(new NameValuePair("rank", ""));
log.info(user.cookieStr);
String jsonString = WebRuquest.post(url, paramlist, user.cookieStr, "");
log.info(jsonString);
if (JsonUtils.getKey(jsonString, "code").equals("100000")) {
log.info(user.getW_useruame_sl() + "成功发表微博。" + ((oldContent.length() > 20) ? (oldContent.substring(0, 20) + "...") : oldContent));
} else if (JsonUtils.getKey(jsonString, "code").equals("100027")) {
log.info(user.getW_useruame_sl() + "发微博失败。不要太贪心哦,发一次就够啦!" + JsonUtils.getKey(jsonString, "msg"));
} else {
log.info(user.getW_useruame_sl() + "发微博失败。" + JsonUtils.getKey(jsonString, "msg"));
log.info(user.getW_useruame_sl() + "发微博失败。返回内容:" + jsonString);
}
}
/**
* 用户名 base64 编码
*
* @param account
* @return
*/
private static String encodeAccount(String account) {
String userName = "";
try {
userName = Base64.encodeBase64String(URLEncoder.encode(account, "UTF-8").getBytes());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return userName;
}
/**
* 生成 Nonce 这个值应该从服务器获取
*
* @param len
* @return
*/
private static String makeNonce(int len) {
String x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
String str = "";
for (int i = 0; i < len; i++) {
str += x.charAt((int) (Math.ceil(Math.random() * 1000000) % x.length()));
}
return str;
}
/**
* 生成 ServerTime 这个值应该从服务器获取
*
* @param len
* @return
*/
private static String getServerTime() {
long servertime = new Date().getTime() / 1000;
return String.valueOf(servertime);
}
...全文
相关推荐
新浪微博话题爬虫
使用c#语言编写的新浪微博话题爬虫,具体介绍参见我的博文。代码使用VS2010编写,具体使用时需要调整存储文件路径等参数。
模拟登录新浪微博的HTTPClient 4库实现
新浪微博的登录流程涉及到用户与服务器之间的多次交互。首先,用户通过浏览器或者客户端向新浪微博的登录页面发送请求,登录页面会检查请求中是否已经携带了有效的登录凭证(如Cookie或者Token)。如果没有,服务器会返回登录页面,让用户输入用户名和密码进行认证。认证通过后,服务器会给用户的浏览器或客户端设置一个带有登录状态的Cookie,用于后续的会话保持。通常,为了提高用户体验,新浪微博会采用HTTPS协议来保证数据传输的安全性,并通过加密手段来保护用户的用户名和密码。
java实现新浪微博第三方登录
用java实现的新浪微博第三发登录的Demo,这只是代码,你要先去新浪开发者中心去开通授权,然后还有自己的域名,再然后就是这个Demo
android新浪微博实例,登录,获取微博信息
android新浪微博OAuth认证登录获取微博内容实例
Asp.Net 腾讯QQ授权登录,新浪微博授权登录,OAuth2
Asp.Net QQ开放平台登录代码,新浪微博开放平台登录代码,OAuth2,腾讯QQ授权登录,新浪微博授权登录,完全源代码,无任何外部dll支持,简单易懂,适合学习及开发应用!


Web 开发
