设为首页 加入收藏

TOP

java利用url解析网页内容并模拟手动form提交数据(二)
2014-11-24 12:02:33 来源: 作者: 【 】 浏览:183
Tags:java 利用 url 解析 网页 内容 模拟 手动 form 提交 数据
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setUseCaches(false);
urlConnection.setDefaultUseCaches(false);
// set request header
if (requestHeaderMap != null) {
for (Map.Entry entry : requestHeaderMap
.entrySet()) {
String key = entry.getKey();
String val = entry.getValue();
if (key != null && val != null) {
urlConnection.setRequestProperty(key, val);
}
}
}
if (isPost) {
urlConnection.setDoOutput(true);
ByteArrayOutputStream bufOut = new ByteArrayOutputStream();
boolean firstParam = true;
for (Map.Entry entry : paramMap.entrySet()) {
String encName = URLEncoder.encode(entry.getKey(), "UTF-8");
if (firstParam) {
firstParam = false;
} else {
bufOut.write((byte) '&');
}
String encValue = URLEncoder.encode(entry.getValue(),
"UTF-8");
bufOut.write(encName.getBytes("UTF-8"));
bufOut.write((byte) '=');
bufOut.write(encValue.getBytes("UTF-8"));
}
byte[] postContent = bufOut.toByteArray();
if (urlConnection instanceof HttpURLConnection) {
((HttpURLConnection) urlConnection)
.setFixedLengthStreamingMode(postContent.length);
}
OutputStream postOut = urlConnection.getOutputStream();
postOut.write(postContent);
postOut.flush();
postOut.close();
}
httpUrlConnection.connect();
int responseCode = httpUrlConnection.getResponseCode();
// We handle redirects ourself
if (responseCode == HttpURLConnection.HTTP_MOVED_PERM
|| responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
String location = httpUrlConnection.getHeaderField("Location");
URL newAction = new URL(url, location);
// Recurse
StringBuffer newUrlSb = new StringBuffer(newAction
.getProtocol()
+ "://" + newAction.getHost());
if (newAction.getPort() != -1) {
newUrlSb.append(":" + newAction.getPort());
}
if (newAction.getPath() != null) {
newUrlSb.append(newAction.getPath());
}
if (newAction.getQuery() != null) {
newUrlSb.append(" " + newAction.getQuery());
}
if (newAction.getRef() != null) {
newUrlSb.append("#" + newAction.getRef());
}
return curl("POST", newUrlSb.toString(), paramMap, requestHeaderMap,
isOnlyReturnHeader,path);
} else if (responseCode == HttpURLConnection.HTTP_OK
|| responseCode == HttpURLConnection.HTTP_CREATED) {
byte[] bytes = new byte[0];
if (!isOnlyReturnHeader) {
if(isPost){
in = httpUrlConnection.getInpu
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Maven使用教程 下一篇java实现u盘指定内容的自动复制

评论

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