首页>代码>apache HttpClient 4.3.4自动登录并抓取中国联通网页用户基本信息和账单数据>/maven-httpclient-demo/src/main/java/com/httpclient/asm/demo/LoginChinaUnicom.java
package com.httpclient.asm.demo;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.Args;
import org.apache.http.util.CharArrayBuffer;
import org.apache.http.util.EntityUtils;
import java.io.*;
import java.nio.charset.*;
import java.text.ParseException;
public class LoginChinaUnicom {
/**
* 登录并抓取中国联通数据
*
* @author Edson.di
* @date 2015年3月4日
* @version 1.0
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String name = "手机联通号码";
String pwd = "手机服务号";
String url = "https://uac.10010.com/portal/Service/MallLogin?callback=jQuery17202691898950318097_1403425938090&redirectURL=http%3A%2F%2Fwww.10010.com&userName="
+ name
+ "&password="
+ pwd
+ "&pwdType=01&productType=01&redirectType=01&rememberMe=1";
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse loginResponse = httpClient.execute(httpGet);
if (loginResponse.getStatusLine().getStatusCode() == 200) {
for (Header heade : loginResponse.getAllHeaders()) {
System.out.println(heade);
}
}
HttpEntity httpentity = loginResponse.getEntity();
String loginEntityContent = EntityUtils.toString(httpentity);
System.out.println("登录状态:" + loginEntityContent);
// 如果登录成功
if (loginEntityContent.contains("resultCode:\"0000\"")) {
// 月份
String months[] = new String[] { "201410", "201411", "201412",
"201501", "201502" };
for (String month : months) {
String billurl = "http://iservice.10010.com/ehallService/static/historyBiil/execute/YH102010002/QUERY_YH102010002.processData/QueryYH102010002_Data/"
+ month + "/undefined";
HttpPost httppost = new HttpPost(billurl);
HttpResponse billresponse = httpClient.execute(httppost);
if (billresponse.getStatusLine().getStatusCode() == 200) {
saveToLocal(billresponse.getEntity(), "chinaunicom.bill."
+ month + ".3.html");
}
}
}
}
/**
* 写文件到本地
*
* @param httpEntity
* @param filename
*/
public static void saveToLocal(HttpEntity httpEntity, String filename) {
try {
File dir = new File("/JEE/sz-588/workspace/maven-httpclient-demo");
if (!dir.isDirectory()) {
dir.mkdir();
}
File file = new File(dir.getAbsolutePath() + "/" + filename);
FileOutputStream fileOutputStream = new FileOutputStream(file);
InputStream inputStream = httpEntity.getContent();
if (!file.exists()) {
file.createNewFile();
}
byte[] bytes = new byte[1024];
int length = 0;
while ((length = inputStream.read(bytes)) > 0) {
fileOutputStream.write(bytes, 0, length);
}
inputStream.close();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 输出简单处理
*/
public static String toString(final HttpEntity entity,
final Charset defaultCharset) throws IOException, ParseException {
Args.notNull(entity, "Entity");
final InputStream instream = entity.getContent();
if (instream == null) {
return null;
}
try {
Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
"HTTP entity too large to be buffered in memory");
int i = (int) entity.getContentLength();
if (i < 0) {
i = 4096;
}
Charset charset = null;
try {
final ContentType contentType = ContentType.get(entity);
if (contentType != null) {
charset = contentType.getCharset();
}
} catch (final UnsupportedCharsetException ex) {
throw new UnsupportedEncodingException(ex.getMessage());
}
if (charset == null) {
charset = defaultCharset;
}
if (charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
}
final Reader reader = new InputStreamReader(instream, charset);
final CharArrayBuffer buffer = new CharArrayBuffer(i);
final char[] tmp = new char[1024];
int l;
while ((l = reader.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
return buffer.toString();
} finally {
instream.close();
}
}
}
最近下载更多
mq13947193109 LV19
2022年7月5日
xxx159 LV1
2021年4月17日
Matrix12312312 LV1
2019年11月13日
z_yong76 LV26
2019年1月10日
chinafjfzlj LV31
2018年12月26日
elan19 LV5
2018年9月21日
潇love梦 LV9
2018年5月21日
deng214 LV9
2018年3月22日
Madman LV10
2017年10月24日
wsz642531 LV4
2017年9月15日
最近浏览更多
2036495585 LV9
2023年9月25日
heqian LV17
2023年1月10日
微信网友_5992582549164032 LV6
2022年12月12日
微信网友_6040315240812544 LV8
2022年11月3日
dayuln LV8
2022年4月27日
有时候简简单单就好了 LV4
2022年4月16日
15227959767
2022年4月14日
暂无贡献等级
cx123123 LV7
2022年2月28日
jackylook
2021年12月10日
暂无贡献等级
lw20020421 LV10
2021年6月22日

