HttpClient 实现快盘签到

本文介绍了一种使用Java模拟POST提交的方式实现快盘自动签到的功能。通过构造HTTP Post请求并设置必要参数,成功实现了登录验证及签到过程。代码中详细展示了如何设置请求头、发送请求体及解析响应。
[quote][b]模拟POST提交实现快盘签到[/b][/quote]




import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
* @author <a href="mailto:foohsinglong@gmail.com">kevin.long</a>
* @description
*/
public class TestKlive {

private static HttpClient hc = new DefaultHttpClient();

public static String postXml(String url, String xmlData) {
String body = null;
try {
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-Type", "text/xml;charset=utf-8");
httppost.setHeader("v", "2");
// 设置参数
httppost.setEntity(new StringEntity(xmlData));
// 发送请求
HttpResponse httpresponse = hc.execute(httppost);
// 获取返回数据
HttpEntity entity = httpresponse.getEntity();
body = EntityUtils.toString(entity);
if (entity != null) {
entity.consumeContent();
}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return body;
}

/**
* @param args
* @throws DocumentException
*/
public static void main(String[] args) throws DocumentException {
String getTokenUrl = "http://api-filesys.wps.cn/xsvr/login/";
String signUrl = "http://point.wps.cn/kpoints/submit/sign/";
StringBuffer xmlData = new StringBuffer();
xmlData.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?><xLive><user>");
xmlData.append("jinshany000@sina.com");
xmlData.append("</user><password>");
xmlData.append("123456");
xmlData.append("</password><deviceId>greenx-a34064b5b75e0c100f52c6c0fe80d887|

GEBINWIN</deviceId><clientName>greenx</clientName><clientVersion>1.0.0.90</clientVersion></xLive>");
System.out.println(xmlData);
String body = postXml(getTokenUrl, xmlData.toString());
System.out.println(body);
Document document = DocumentHelper.parseText(body);
Element rootElt = document.getRootElement();
String userId = rootElt.elementTextTrim("userId");
String token = rootElt.elementTextTrim("token");
System.out.println("获取信息\t"+userId+":"+token);
xmlData.delete(0, xmlData.length());
xmlData.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?><xLive><token>");
xmlData.append(token);
xmlData.append("</token><userId>");
xmlData.append(userId);
xmlData.append("</userId></xLive>");
body = postXml(signUrl, xmlData.toString());
System.out.println(body);
}
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值