设为首页 加入收藏

TOP

java利用url解析网页内容并模拟手动form提交数据(三)
2014-11-24 12:02:33 来源: 作者: 【 】 浏览:182
Tags:java 利用 url 解析 网页 内容 模拟 手动 form 提交 数据
tStream();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
while (true) {
int rc = in.read(buf);
if (rc <= 0) {
break;
} else {
bout.write(buf, 0, rc);
}
}
bytes = bout.toByteArray();
in.close();
}else{
DataInputStream ins = new DataInputStream(httpUrlConnection
.getInputStream());
//验证码的位置
DataOutputStream out = new DataOutputStream(new FileOutputStream(
path+"/code.bmp"));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = ins.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
out.close();
ins.close();
}
}
// only fetch Content-Length and Last-Modified header
String encoding = null;
if (encoding == null) {
encoding = getEncodingFromContentType(httpUrlConnection
.getHeaderField(CONTENT_TYPE));
}
content = new Content(sUrl, new String(bytes, encoding),
httpUrlConnection.getHeaderFields());
}
} catch (Exception e) {
return null;
} finally {
if (httpUrlConnection != null) {
httpUrlConnection.disconnect();
}
}
return content;
}
public static String getEncodingFromContentType(String contentType) {
String encoding = null;
if (contentType == null) {
return null;
}
StringTokenizer tok = new StringTokenizer(contentType, ";");
if (tok.hasMoreTokens()) {
tok.nextToken();
while (tok.hasMoreTokens()) {
String assignment = tok.nextToken().trim();
int eqIdx = assignment.indexOf('=');
if (eqIdx != -1) {
String varName = assignment.substring(0, eqIdx).trim();
if ("charset".equalsIgnoreCase(varName)) {
String varValue = assignment.substring(eqIdx + 1)
.trim();
if (varValue.startsWith("\"")
&& varValue.endsWith("\"")) {
// substring works on indices
varValue = varValue.substring(1,
varValue.length() - 1);
}
if (Charset.isSupported(varValue)) {
encoding = varValue;
}
}
}
}
}
if (encoding == null) {
return "UTF-8";
}
return encoding;
}
public static void main(String[] args) {
// login
//System.out.println(getHtmlReadLine("http://www.zhaopin.com/"));
//验证码的位置
Content content = curl("GET", "http://www.haoshijia.com.cn/register/index/verify", null, null, false,"d:/");
// build request header
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Maven使用教程 下一篇java实现u盘指定内容的自动复制

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: