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

被折叠的 条评论
为什么被折叠?



