抓取表单请求数据

 

基于网络流的方式读取特定URL资源。

通过网络流连接(URLConnection)对象,设定请求相关属性;根据表单提交方式(POST|GET)设置远程网络流请求方式;通过网络输出流对象追加请求参数列表;同时,需注意要设定是否打开网络输出流对象以便读取返回的网络流数据;

 

package org.fanzone.net;

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

 

/** 

 * Title: base<br> 

 * Description: Grab Data from URL resource.<br> 

 * Copyright: Copyright (c) 2011 <br> 

 * Create DateTime: Jul 7, 2011 5:41:07 PM <br> 

 * @author wangmeng

 */

public class Grab {

 

/**

* Read out stream and fill it into a specified file.

* @param is

* @param filePath

*/

private static void readerHandle(InputStream is, String filePath){

BufferedReader br = null;

BufferedWriter bw = null;

File file = null;

String content = "";

try {

file = new File(filePath);

if(file.exists()){

file.delete();

}

file.createNewFile();

br = new BufferedReader(new InputStreamReader(is));

bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));

while(null != (content = br.readLine())){

bw.append(content);

}

bw.close();

br.close();

Runtime.getRuntime().exec("explorer " + filePath + "");

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args){

final String TARGET_URL = "http://www.SunnySoLong.com/net/grab.do";

URL url = null;

HttpURLConnection connection = null;

String linkParams = "";

try {

url = new URL(TARGET_URL);

connection = (HttpURLConnection) url.openConnection();

connection.setRequestProperty("Connection","close");

connection.setRequestProperty("Keep-Alive","close");

connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; (R1 1.5); .NETSPX2; .NET CLR 2.0.50727)");

connection.setRequestProperty("Cache-Control","no-cache");

connection.setRequestMethod("POST");

connection.setDoOutput(true);

linkParams="man=sun&woman=moon";

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

connection.setRequestProperty("Content-Length", "" + (linkParams.getBytes()).length );

connection.setRequestProperty("Referer",TARGET_URL);

 

/** Append Parameters */

    OutputStream output = connection.getOutputStream();

    output.write(linkParams.getBytes());

    output.flush();

    output.close();

 

    connection.connect();

    System.out.println(connection.getResponseCode());

System.out.println("start ...");

readerHandle(connection.getInputStream(),"C:\\grab.html");

System.out.println("end ...");

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值