HttpClient 实现快盘签到

引用
模拟POST提交实现快盘签到




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:[email protected]">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("[email protected]");
		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);
	}
}

你可能感兴趣的:(httpclient)